简体   繁体   中英

Why do I receive old data in my gridview on page load?

The process I am doing is if lbl.Text is "Validated" then disable the checkbox accordingly in grid. The code works fine if paging is not there. Now the problem is I am using paging and when I click to the next page of grid the validated things appear with the checkbox enabled.

I checked through breakpoints. Its loading the previous gridpage values during page load event. And after pageloadevent its going design and loads the new values in 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;
        }
    }
}

I think you need to do enable or disable each of the checkboxes individually in the GridView.RowDataBound event rather than all at once in the Page_Load event:

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";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM