Why am I getting old data in my gridview when the page loads?

The process I am doing is that if it lbl.Textis "Validated" then uncheck the box accordingly in the grid. The code works fine if there is no paging. Now the problem is that I use paging, and when I click on the next grid page, the checked things are displayed with the checkbox turned on.

I checked through breakpoints. It loads the previous grid values ​​during page load. And after pageloadevent starts its development and loads the new values ​​into the grid.

//-------loading previous page values of grid here---------

protected void Page_Load(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        Label lbl = (Label)row.FindControl("Labely8");
        Label Label23 = (Label)row.FindControl("Label23");
        CheckBox checkbox = (CheckBox)row.FindControl("chkRows");
        if (lbl.Text == "Validated")
        {
            checkbox.Enabled = false;
        }
        else
        {
            checkbox.Enabled = true;
        }
    }
}
+3
source share
1 answer

, GridView.RowDataBound , Page_Load :

void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{    
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        CheckBox checkbox = (CheckBox)e.Row.FindControl("chkRows");
        checkbox.Enabled = e.Row.Cells["nameOfCellWithLabel"].Text == "Validated";
    }
}
+2

All Articles