ASP. NET learning: How to return to the previous page

Source: Internet
Author: User
Back to the previous page, this stuff is usually used when we make a project and confirm after entering the form. It is used to modify the original input data, or because a website is designed to make it easier for viewers, this function is generally implemented using a button control in Asp.net. There are many implementation methods, today, I used this stuff when I was working on a project. I summarized several methods that can implement "back to previous page" and "back to previous page" for your learning, please advise: 0)

In fact, JavaScript is mainly used to implement this function.

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)

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
}

The following section from: http://stswordman.cnblogs.com/archive/2006/06/12/423910.html
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 5

This method is also troublesome and is not recommended. It seems to be in ASP.

<A href = <% = request. servervariable ("http_referre) %>

<Asp: Image id = "imageback" visible = true "imagurl =" Previous Page "runat =" server "/> </a>

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.
Let's look at this. I hope you can correct it more :)

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.