Implementation of returning to the previous page using js in asp.net

Source: Internet
Author: User

Method 1:
In the source code of aspx in asp.net
<Input type = "button onclick =" javascript: window. history. go (-1); "value =" back to previous page ">
Analysis: This is an HTML control. Through an onclick event, you can call a method in javascript. This is the simplest and also applies to static pages and ASP pages.
Method 2:
Use Reponse. write
If you have a certain understanding of ASP, then the Response. write is no stranger to this stuff. The first method is to implement it directly on an HTML page, then it is implemented in the background environment (this statement does not seem very standard, huh, huh) Copy codeThe Code is as follows: Response. write ("<script language = javascript> history. go (-2); </script>)
<A href = "#" onclick = "javascript: history. back ();"> return to the previous page </a>

Here we will use the-2 value. I personally think it is like this: Because in asp.net, when you press a button, the page will be implemented. because of postback, the page is actually refreshed twice, and we want it for the first time, so it is ......

Method 3
Use Response. Redirect () or server. transfer ()

Add in page_load
If (! IsPostBack)
ViewState ["retu"] = Request. UrlReferrer. ToString ();
In the return button event
Response. Redirect (ViewState ["retu"]. ToString ());
Or Server. Transfer (ViewState ["retu"]. ToString ());

Analysis:
Request. UrlReferrer can obtain the url Information of the client's last Request. We recommend that you make a judgment when using this information.
If (ViewState ["UrlReferrer"]! = Null)
Response. Redirect (ViewState ["UrlReferrer"]. ToString ();
Else
{
Response. write ("sorry, the current is the front page number");

So that we can make it a little bit
}
Note the following when using Request. UrlReferrer:
1. If the document. location method is used on the previous page to navigate to the current page, Request. UrlReferrer returns a null value.
2. If there are two pages A and B, directly Request page A in the browser and navigate to page B in the Page_Load event of page A, then Request. UrlReferrer returns NULL. Because the page has not been initialized in the Page_load event, you cannot record the information of the current page. If you navigate to page B, you cannot obtain the information of the previous page.
3. clicking the refresh button will not change Request. UrlReferrer

Method 4:
Very few people use this method, but I tried it and it was quite good.
In The onClick event of the button, enter
This. RegisterClientScriptBlock ("e", "<script language = javascript> history. go (-2); </script> ");
Returns to the previous page.

Method 5Copy codeThe Code is as follows: <a href = <% = request. servervariable ("http_Referre) %>
<Asp: image id = "imageback" visible = true "imagurl =" Previous Page "runat =" server "/> </a>

Method 6Copy codeThe Code is as follows: Response. Write ("<script> alert ('prompt information'); history. back () </script> ");
Response. Write ("<script> alert ('prompt information'); top. location. href = '" + PageURL + "' </script> ")
Response. Write ("<script> alert ('prompt information'); window. location. href = '" + PageURL + "' </script> ");
Response. Write ("<script> alert ('prompt information'); window. location = window. location; </script> ");

Here, I also want to use Javascript to implement and use Response. redirect () is a little different. If you want to retain the data you entered when using a daily form, you need to use history. go (); you can use reponse if you only return directly without any operation. redirect.

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.