Comparison and Selection of ASP. NET page navigation methods (1)

Source: Internet
Author: User

In ASP. NET applications, there are multiple ways to navigate between Web forms: Using Hyperlink, using Response. Redirect, using Server. Transfer, or using Server. Execute. This article analyzes the similarities and differences between the four navigation methods and their advantages and disadvantages to help you select the best navigation method.
1. hyperlink
The simplest way to enter another form from a form is to use the HTML hyperlink control. In Web forms, HTML code classes that use hyperlinks are as follows:Go to Form 2
When the user clicks this hyperlink, WebForm2.aspx executes and sends the result to the browser. The hyperlink navigation method can be used almost anywhere, including HTML pages and common ASP pages. ASP. NET also provides another alternative method, namely, the HyperLink Server Control:
NavigateUrl = "WebForm2.aspx"> enter Form 2The preceding HTML code runs the same result as the first example, because ASP. NET regards the HyperLink Web server control as an HTML HyperLink control. But there is one important difference between the two. HyperLink Web server controls can be programmed on the server side. Specifically, you can change the NavigateUrl attribute in the program code to allow the construction of a hyperlink that can dynamically change according to the current state of the application. For example:
Private Sub button#click (_
ByVal sender As System. Object ,_
ByVal e As System. EventArgs )_
Handles Button1.Click
HyperLink1.NavigateUrl = "WebForm3.aspx"
End Sub
After the code is executed, if the user clicks the link, the user will see WebForm3.aspx instead of WebForm2.aspx.

2. Use a program to control redirection
Although hyperlink can navigate from one page to another, this navigation method is completely controlled by the user. Sometimes, we may use code to control the entire navigation process, including when to go to another page. In these cases, ASP. NET has three different methods to achieve similar goals: to call the Redirect method of the Response object, to call the Transfer or Execute method of the Server object. The behavior of these three navigation methods is similar, but there are also differences.

2.1 Response. Redirect
The Response. Redirect method causes the browser to link to a specified URL. When the Response. Redirect () method is called, it creates a Response. The Response header indicates that status code 302 indicates that the target has changed) and a new target URL. The browser receives the response from the server and sends a request to the new URL using the information in the response header.
This means that Response is used. in the Redirect method, the redirection operation occurs on the client and involves two communications with the server): the first request to the original page, and a 302 response is obtained, the second is the new page stated in the 302 response to the request, and the page after the redirection is obtained.

2.2 Server. Transfer
The Server. Transfer Method transfers the execution process from the current ASPX file to another ASPX page on the same Server. When Server. Transfer is called, the current ASPX page is terminated and the execution process is transferred to another ASPX page. However, the new ASPX page still uses the response stream created on the previous ASPX page. If you use the Server. Transfer method to implement navigation between pages, the URL in the browser will not change because the redirection is completely performed on the Server side, and the browser does not know that the Server has executed a page transformation.
By default, Server. the Transfer method does not pass form data or query strings from one page to another, but you only need to set the second parameter of this method to True, the form data and query string of the first page can be retained.
At the same time, use Server. note that the target page will use the response stream created on the original page, which leads to ASP. NET Machine Authentication Check, MAC) that the ViewState of the new page has been tampered. Therefore, to retain the form data and query string set of the original Page, you must set the EnableViewStateMac attribute of the target Page command to False.


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.