C# – viewstate disappears on response.redirect

asp.netcviewstate

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?

Best Answer

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.

Related Topic