We are writing ASP-based. if an error occurs in code execution or an exception is detected, the user is usually prompted to "return" or "return ", you can also provide a link to the previous page in the multi-step operation, list, or detailed view interface. In this case, the simple method that people will soon think of is to use Javascript to implement it, that is, history. go (-1), but because ASP.. NET page PostBack mechanism, so history. go (-1) may still be the current page, but 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 ()... {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