Reprinted-methods for processing page "rollback" in ASP. NET

Source: Internet
Author: User

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

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.