Click here to Skip to main content
15,885,918 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Gridview disappear click edit field in the gridview and click update button. 

if remove ispostback from page load and run the solution then click update button, gridview is not dissappear but previous values coming from gridview, if i check with break point


What I have tried:

Source COde
___________


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Flow.aspx.cs" Inherits="WebApplication1.Flow" %>

<!DOCTYPE html>



    <title>
    

.headerstyle
{
color:#FFFFFF;border-right-color:#abb079;border-bottom-color:#abb079;background-color: #df5015;padding:0.5em 0.5em 0.5em 0.5em;text-align:center;
}
    


    
    <div>
    <asp:TextBox ID="txtpname" runat="server" /><asp:TextBox ID="txtprice" runat="server" />
<asp:Button ID="btnAdd" runat="server" Text="Add Product" OnClick="btnAdd_Click" />
    </div>
<asp:GridView runat="server" ID="gvDetails" ShowFooter="true" AllowPaging="true" PageSize="10"
     AutoGenerateColumns="false" DataKeyNames="productid,productname,price"
     OnPageIndexChanging="gvDetails_PageIndexChanging" OnRowCancelingEdit="gvDetails_RowCancelingEdit"
OnRowEditing="gvDetails_RowEditing" OnRowUpdating="gvDetails_RowUpdating" 
    OnRowDeleting="gvDetails_RowDeleting" EnableViewState="False"
      >
<HeaderStyle CssClass="headerstyle" />
<columns>
<asp:BoundField DataField="productid" HeaderText="Product Id" ReadOnly="true" />
<asp:TemplateField HeaderText="Product Name">
<itemtemplate>
<asp:Label ID="lblProductname" runat="server" Text='<%# Eval("productname")%>'/>

<edititemtemplate>
<asp:TextBox ID="txtProductname" runat="server" Text='<%# Eval("productname")%>'/>


<asp:TemplateField HeaderText = "Price">
<itemtemplate>
<asp:Label ID="lblPrice" runat="server"  Text='<%# Eval("price")%>'>

<edititemtemplate>
<asp:TextBox ID="txtProductprice" runat="server" Text='<%# Eval("price")%>'/>


<asp:CommandField ShowEditButton="True" ShowDeleteButton="true" />


    



Flow.aspx.cs
______________

<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication1.Account;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class Flow : System.Web.UI.Page
    {
        BEL objUserBEL = new BEL();
        string Output = string.Empty;
        DataSet ds=new DataSet();
        BLL objUserBLL = new BLL();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindGridview();
            }
            
            
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {

            objUserBEL.productname = txtpname.Text;
            objUserBEL.price = txtprice.Text;
            objUserBEL.op_type = "INSERT";
            Output = objUserBLL.InsertUserDetails(objUserBEL);
        }
        protected void BindGridview()
        {
            objUserBEL.op_type = "SELECT";
            
            ds = objUserBLL.GetUserDetails(objUserBEL);
            if (ds.Tables[0].Rows.Count > 0)
            {
                gvDetails.DataSource = ds;
                gvDetails.DataBind();
            }
            else
            {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                gvDetails.DataSource = ds;
                gvDetails.DataBind();
                int columncount = gvDetails.Rows[0].Cells.Count;
                gvDetails.Rows[0].Cells.Clear();
                gvDetails.Rows[0].Cells.Add(new TableCell());
                gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
                gvDetails.Rows[0].Cells[0].Text = "No Records Found";
            }
        }
        protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvDetails.EditIndex = e.NewEditIndex;
            BindGridview();
        }
        protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvDetails.EditIndex = -1;
            BindGridview();
        }
        protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDetails.PageIndex = e.NewPageIndex;
            BindGridview();
        }
        protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int productid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["productid"].ToString());
            string productname = gvDetails.DataKeys[e.RowIndex].Values["productname"].ToString();
            string price = gvDetails.DataKeys[e.RowIndex].Values["price"].ToString();
            objUserBEL.price = price;
            objUserBEL.productname = productname;
            objUserBEL.op_type="UPDATE";
            objUserBEL.productid=productid;
            Output=objUserBLL.updatecrudoperations(objUserBEL);
            BindGridview();
           
        }
        protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int productid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["productid"].ToString());
            string productname = gvDetails.DataKeys[e.RowIndex].Values["productname"].ToString();
            
            objUserBEL.price=txtprice.Text;
            objUserBEL.productname=productname;
            objUserBEL.op_type="DELETE";
            objUserBEL.productid=productid;
            Output=objUserBLL.deletecrudoperations(objUserBEL);
            BindGridview();
        }
       
        }

    }
Posted
Updated 8-Nov-18 5:40am
v2
Comments
Herman<T>.Instance 30-Oct-17 7:56am    
Pretty obscure code!!!
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvDetails.DataSource = ds;
gvDetails.DataBind();
int columncount = gvDetails.Rows[0].Cells.Count;
gvDetails.Rows[0].Cells.Clear();
gvDetails.Rows[0].Cells.Add(new TableCell());
gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
gvDetails.Rows[0].Cells[0].Text = "No Records Found";
}
When there is no data the gridview component can set a message by using the property EmptyDataText. So you don't have to check if you really have data or not.
See: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatext(v=vs.110).aspx.

The best thing you would do for this situation is to debug your code. Set a break-point to your BindGridView() method after hitting the RowUpdating event then step-into your code. Check the datasource returned from this line: ds = objUserBLL.GetUserDetails(objUserBEL);

Keep in mind that a data representation control such as GridView will only display data and rendered in the page if there's data on the datasource that you set.
 
Share this answer
 
Private Sub gvDetails_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gvDetails.RowCommand
BindGridview()
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900