Methods for processing page "fallback" in asp.net

Source: Internet
Author: User
Tags implement net return

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



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.