Reposted http://www.cnblogs.com/ghd258/archive/2006/10/18/532047.html,gao Haidong's blog
We are writing ASP. NET-based
If an error occurs in code execution or an exception is detected, the user is usually prompted to "return" or "back", or on the multi-step operation, list, or detailed Viewing Interface, it also provides the user with a rollback to the previous page.
In this case, you will soon think of the simple method of using JavaScript, that is, history. Go (-1 ).
The PostBack mechanism of ASP. NET pages, so history. Go (-1) may still be the current page, but it cannot actually roll back to the previous page.
In classifieds site starter kit, I learned a good way to back the page. I can implement page rollback in the client and server controls respectively. The Code is as follows:
1) First, add two attributes to the page.
// Record the information of the previous page
Private string urlreferrer
...{
Get ...{
Return viewstate ["urlreferrer"] as string;
} Set ...{
Viewstate ["urlreferrer"] = value;
}
}
// Record the number of PostBack times Public int numpostbacks
...{
Get ...{
If (viewstate ["numpostbacks"]! = NULL)
Return (INT) viewstate ["numpostbacks"];
Else ...{
Viewstate ["numpostbacks"] = 0;
Return 0;
} Set ...{
Viewstate ["numpostbacks"] = value;
}}
2) record the previous page address, the number of PostBack updates, and the address of the rollback link in the page_load event.
// Record previous page information or the number of PostBack updates protected void page_load (Object sender, eventargs E)
...{
If (! Page. ispostback)
...{
If (request. urlreferrer! = NULL)
This. urlreferrer = request. urlreferrer. tostring ();
} Else numpostbacks ++;
Int gobacksteps = numpostbacks + 1;
Backlink. navigateurl = string. Format ("javascript: history. Go (-{0});", gobacksteps );
}
3) directly process the rollback operation (such as back_click) in the code. You can directly call the following method:
// Return protected void returntopreviouspage () in the code ()
...{
String referrer = urlreferrer;
If (referrer! = NULL)
Response. Redirect (referrer );
Else response. Redirect ("~ /Default. aspx ", true );
}
From: http://blog.joycode.com/moslem/archive/2006/10/17/85307.aspx