Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
        }
    }
}
like image 833
Rockin Avatar asked Dec 08 '25 09:12

Rockin


1 Answers

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";
    }
}
like image 172
fretje Avatar answered Dec 09 '25 23:12

fretje



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!