2

have a GridView but cannot retrieve any of the data on post-back. I have set a break point at the start of Page_Init and Page_Load, on any post-back that is triggered the GridView.Rows.Count property is always 0. The GridView is defined as shown below:

        <asp:GridView ID="TestGrid" runat="server" AllowSorting="False" DataKeyNames="ID,stock,percentage"
            OnRowDataBound="GridViewTest_RowDataBound" EnableViewState="True" 
            OnRowUpdating="GridViewTest_OnRowUpdating" OnRowEditing="GridViewTest_OnRowEditing" OnRowCancelingEdit="GridViewTest_OnRowCancelingEdit"
            AutoGenerateColumns="False">
            <Columns>
                <asp:CommandField ShowEditButton="True" CausesValidation="False"/>
                <asp:BoundField DataField="Client" HeaderText="Client" InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="Stock" HeaderText="Stock" InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="DutyStatus" HeaderText="DutyStatus" InsertVisible="False" ReadOnly="True" />
                <asp:TemplateField HeaderText="Percentage" InsertVisible="False">
                    <ItemTemplate>
                        <asp:Label ID="enhLbl" runat="server"><%#Eval("Percentage")%></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="TbPercentage" runat="server" Text="<%#Bind("Percentage")%>" class="edit-field"></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

I have a check for IsPostBack in Page_Load and only bind the GridView to its DataSource if it's not a post-back. The only other time it gets re-bound is in the GridViewTest_OnRowEditing, GridViewTest_OnRowUpdating and GridViewTest_OnRowCancelingEdit event handlers.

I am binding the GridView to a DataTable in the following way:

    AGridView.DataSource = DataTableSplitParcels;
    AGridView.DataBind();
    AGridView.EnableViewState = true;

I don't know if it makes any difference, but the DataTable is created in code and the rows added within a loop.

Within Page_Load on a post-back I can see the GridView has its DataKeysArrayList property populated with the current values, however with it having no rows I am unable to retrieve any updated values.

9
  • When you are getting your form postback then why are you not binding your DataGrid? At that time, this grid will be loaded empty, of course. How would it persist value when this control is getting populated as fresh control? Sep 16, 2015 at 11:14
  • How would I ever get the updated value in GridViewTest_OnRowUpdating? all the examples online show the value being pulled from the row using the GridViewUpdateEventArgs.RowIndex value. Sep 16, 2015 at 11:26
  • I am not rebinding so I don't lose the updated value Sep 16, 2015 at 11:26
  • Are you trying to edit your GridView (or updating data in gridview on the fly) and then you want to process it at server side before you shoot it to DB? and on final page, latest data should appear? Sep 16, 2015 at 11:42
  • Basically yes, however I can never get access to the updated value from TbPercentage because that control and all the rows in the GridView no longer exist on post-back even though I am not re-binding it. Sep 16, 2015 at 11:45

1 Answer 1

0

The problem seems in the Page_Load event, which is refreshing your GridView with every call (irrespective of postback), and because of that you are losing what was entered by the user during update.

So put a debugger on your Page_Load and BindData events and see if it is getting called by Page_Load at all, and you need to stop that.. I tried a sample app and it worked at my end.. see below the code that I tried.

ASPX

<asp:GridView ID="TaskGridView" runat="server" 
        AutoGenerateEditButton="True" 
        AllowPaging="true"
        OnRowEditing="TaskGridView_RowEditing"         
        OnRowCancelingEdit="TaskGridView_RowCancelingEdit" 
        OnRowUpdating="TaskGridView_RowUpdating"
        OnPageIndexChanging="TaskGridView_PageIndexChanging">
      </asp:GridView>

CS

protected void Page_Load(object sender, EventArgs e)
{

    if (!Page.IsPostBack)
    {
        // Create a new table.
        DataTable taskTable = new DataTable("TaskList");

        // Create the columns.
        taskTable.Columns.Add("Id", typeof(int));
        taskTable.Columns.Add("Description", typeof(string));
        taskTable.Columns.Add("IsComplete", typeof(bool));

        //Add data to the new table.
        for (int i = 0; i < 20; i++)
        {
            DataRow tableRow = taskTable.NewRow();
            tableRow["Id"] = i;
            tableRow["Description"] = "Task " + i.ToString();
            tableRow["IsComplete"] = false;
            taskTable.Rows.Add(tableRow);
        }

        //Persist the table in the Session object.
        Session["TaskTable"] = taskTable;

        //Bind data to the GridView control.
        BindData();
    }
}

protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    //Retrieve the table from the session object.
    DataTable dt = (DataTable)Session["TaskTable"];

    //Update the values.
    GridViewRow row = TaskGridView.Rows[e.RowIndex];
    dt.Rows[row.DataItemIndex]["Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;

    //Reset the edit index.
    TaskGridView.EditIndex = -1;

    //Bind data to the GridView control.
    BindData();
}

private void BindData()
{
    TaskGridView.DataSource = Session["TaskTable"];
    TaskGridView.DataBind();
}

Source available here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.