GridView doesn't remember state between postbacks

Posted by Ryan on Stack Overflow See other posts from Stack Overflow or by Ryan
Published on 2010-05-13T18:02:08Z Indexed on 2010/05/14 8:24 UTC
Read the original article Hit count: 652

Filed under:
|
|
|

Hi,

I have a simple ASP page with databound grid (bound to an object source). The grid is within the page of a wizard and has a 'select' checkbox for each row.

In one stage of the wizard, I bind the GridView:

protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
    {
...
        // Bind and display matches
        GridViewMatches.EnableViewState = true;
        GridViewMatches.DataSource = getEmailRecipients();
        GridViewMatches.DataBind();

And when the finish button is clicked, I iterate through the rows and check what's selected:

protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
    // Set the selected values, depending on the checkboxes on the grid.
    foreach (GridViewRow gr in GridViewMatches.Rows)
    {
        Int32 personID = Convert.ToInt32(gr.Cells[0].Text);
        CheckBox selected = (CheckBox) gr.Cells[1].FindControl("CheckBoxSelectedToSend");

But at this stage GridViewMatches.Rows.Count = 0! I don't re-bind the grid, I shouldn't need to, right? I expect the view-state to maintain the state. (Also, if I do rebind the grid, my selection checkboxes will be cleared)

NB: This page also dynamically adds user controls in OnInit method. I have heard that it might mess with the view state, but as far as I can tell, I am doing it correctly and the viewstate for those added controls seems to work (values are persisted between postbacks)

Thanks a lot in advance for any help!

Ryan

UPDATE: Could this be to do with the fact I am setting the datasource programatically? I wondered if the asp engine was databinding the grid during the page lifecycle to a datasource that was not yet defined. (In a test page, the GridView is 'automatically' databound'. I don't want the grid to re-bound I just want the values from the viewstate from the previous post!

Also, I have this in the asp header: ViewStateEncryptionMode="Never" - this was to resolve an occasional 'Invalid Viewstate Validation MAC' message

Thanks again

Ryan

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about viewstate