when we're writing an asp.net based application, if the code executes an error or detects an exception, the user is generally prompted to "return" or "rewind", or in a multi-step operation, a list/Detailed view interface, also gives the user a link back to the previous page, The simple idea that everyone will soon think of is to use Javascript to implement, that is, History.go (-1), but because of the postback mechanism of the ASP.net page, History.go (-1) may still be the current page, not really back to the previous page.
In classifieds Site Starter Kit, learn a good deal about fallback, you can implement the page in the client and server control, the code is as follows:
1 First add two attributes to the page
Record information on the previous page
private String Urlreferrer
... {
Get
... {
return viewstate["Urlreferrer"] as String;
}
Set
... {
viewstate["Urlreferrer"] = value;
}
}
Number of postback recorded
public int Numpostbacks
... {
Get
... {
if (viewstate["numpostbacks"]!= null)
return (int) viewstate["Numpostbacks"];
Else
... {
viewstate["numpostbacks"] = 0;
return 0;
}
}
Set
... {
viewstate["numpostbacks"] = value;
}
}
Record the information on the previous page or update the number of postback
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);
}
Fallback in code
protected void Returntopreviouspage ()
... {
string referrer = Urlreferrer;
if (referrer!= null)
Response.Redirect (referrer);
Else
Response.Redirect ("~/default.aspx", true);
3) directly in the code to handle the fallback operation (such as Back_click), you can call the following methods directly
2 in the Page_Load event record previous page address, update postback times, set the address of the fallback link