Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewstate disappears on response.redirect

On my asp.net c# page I have two text boxes(start and end dates) with ajax CalendarExtenders. The user selects a start date and then an end date. On selecting the end date, I bind my grid as shown below;

 protected void calEndDate_TextChanged(object sender, EventArgs e)
    {
        BindGrid();
    }

In the grid I have a command button with the following code

 protected void gvAllRoomStatus_RowCommand(object sender, GridViewCommandEventArgs e)
    {    
        if (e.CommandName == "Manage")
        {    
            GridViewRow row = gvAllRoomStatus.Rows[Convert.ToInt16(e.CommandArgument)];    
            int BookingID = Convert.ToInt32(row.Cells[1].Text);              

            DataClassesDataContext context = new DataClassesDataContext();
                Session["BookingID"] = BookingID;
                Response.Redirect("CheckIn.aspx");
        }
    }

When the user goes to that page and clicks the back button all the selected dates and the gridview data disappears. Any ideas why the viewstate is disappearing?

like image 202
rumi Avatar asked Dec 16 '22 15:12

rumi


2 Answers

ViewState belongs to the current Page.

Have a look: http://www.codeproject.com/Articles/37753/Access-ViewState-Across-Pages

Yes, we can access the viewstate variables across pages. This is only possible if Cross Page Posting or Server.transfer is used to redirect the user to other page. If Response.redirect is used, then ViewState cannot be accessed across pages.

So you could use Server.Transfer instead or use the Session.

like image 101
Tim Schmelter Avatar answered Jan 03 '23 11:01

Tim Schmelter


Viewstate to look at it in a very simplified way is to see it as a carbon copy or cache of the last state of the page you are currently on. Therefore doing a redirect to any page, even the same page itself, is essentially a fresh start. The viewstate no longer applies as for all intent and purpose, you are on a new page.

As Tim suggests in his post, either store the required data as a session variable or use a server.transfer.

Take a look here for a nice overview of viewstate: http://www.codeproject.com/Articles/37753/Access-ViewState-Across-Pages

like image 23
Phill Healey Avatar answered Jan 03 '23 12:01

Phill Healey