Viewstate disappears on response.redirect
In my asp.net C # page, I have two text boxes (start and end dates) with ajax CalendarExtenders. The user selects a start date and an end date. When choosing an end date, I anchor my grid like 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 navigates to this page and clicks the Back button, all the selected dates and gridview data disappear. Any ideas on which point of view is disappearing?
source to share
ViewState
belongs to the current one Page
.
Take a look: http://www.codeproject.com/Articles/37753/Access-ViewState-Across-Pages
Yes, we can access viewstate variables on all pages. This is only possible if Cross Post or Server.transfer is used to redirect the user to another page. If Response.redirect is used, then the ViewState cannot be accessed through the pages .
So, you can use Server.Transfer
or use Session
.
source to share
Viewstate, to look at it in a very simplistic way, is to see it as a copy or cache of the last state of the page you are currently in. Therefore, switching to any page, even the same page, is essentially a new beginning. Viewstate no longer applies as for all intents and purposes you are on a new page.
As Tim says in his post, either store the required data as a session variable or use server.transfer.
Take a look here for a good overview of the viewstate: http://www.codeproject.com/Articles/37753/Access-ViewState-Across-Pages
source to share
In my opinion the problem is that you are doing autoresponders with calEndDate_TextChanged
Ajax.
After submission, when you press the back button, the browser cannot remember, you cannot save what you changed with all autorun data via Ajax calls and you will lose it.
For me, remove the automatic post to change the text, remove the Ajax because you don't have to, and do a regular full post back when the user submits their details.
Then when you go to the browser, the browser loads the previous state and most browsers remember that and all the user's input. Also on this background the viewstate is the same as the previous one because it hasn't changed since Ajax.
source to share