Several methods and comparisons of Asp.net page navigation

Source: Internet
Author: User

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:
Copy codeThe Code is as follows:
<A href = "WebForm2.aspx"> enter Form 2 </a>

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:
Copy codeThe Code is as follows:
<Form id = "Form1" method = "post" runat = "server">
<Asp: HyperLink id = "HyperLink1" runat = "server"
NavigateUrl = "WebForm2.aspx"> go to Form 2 </asp: HyperLink>
</Form>

The 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:
Copy codeThe Code is as follows:
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 the status code 302 (indicating that the target has changed) and the 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, involving two communications with the server (two back and forth) in total: 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), The ViewState of the new page is deemed to have 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.
2.3 Server. Execute
Server. the Execute method allows the current ASPX page to Execute a specified ASPX page on the same Web Server. When the specified ASPX page is executed, the control process returns to the original page to issue the Server. the location of the Execute call.
This page navigation method is similar to calling the callback function for the ASPX page. The called page can access the form data and query string set of the calling page, therefore, set the EnableViewStateMac attribute of the called Page command to False.
By default, the output of the called page is appended to the current response stream. However, Server. the Execute method has an overloaded method that allows a TextWriter object (or its sub-object, such as the StringWriter object) to obtain the output of the called page, rather than directly appending it to the output stream, on the original page, you can easily adjust the location of the output result of the called page.
To illustrate the working process, we will create a Web form, place a button control (Button1) and a text control (Literal1), transfer it to the Code view on the design interface, and add a System. the Imports statement of the IO namespace, and then add the code executed when the user clicks the button:
Copy codeThe Code is as follows:
Private Sub button#click (_
ByVal sender As System. Object ,_
ByVal e As System. EventArgs )_
Handles Button1.Click
Dim sw As StringWriter = New StringWriter ()
Server. Execute ("WebForm2.aspx", sw)
Literal1.Text = sw. ToString ()
End Sub

Create the second WebForm2.aspx page for the same Web application. Go to the HTML view of the Page and modify its Page command to disable ViewState check:
Copy codeThe Code is as follows:
<% @ Page Language = "vb" AutoEventWireup = "false" Codebehind = "WebForm2.aspx. vb"
Inherits = "Navigate. WebForm2" EnableViewStateMac = "false" %>

Go to the design view and add some controls for the second page. Next, set the first page to the default page to start the application. Click the button to display the WebForm2 control where the Literal button is placed in WebForm1. Note that the page title and URL still display the original page WebForm1.
Use Server. transfer or Server. when using the Execute method to implement navigation, note that the final page may not be a legal HTML page, because the page that is finally returned to the client may contain multiple <HTML> and <BODY> tags. Internet Explorer seems to be able to tolerate and handle such cases correctly, but if you want to use other browsers, you 'd better test it carefully.
Iii. Comparison and Selection
Since there are so many ways to navigate from one page to another, how should we select the best navigation method? The following are some considerations:
· If you want users to decide when to convert a page and which page to go to, hyperlink is the most suitable.
· If you want to use a program to control the conversion target, but the conversion time is determined by the user, use the HyperLink control of the Web server to dynamically set its NavigateUrl attribute.
· If you want to connect a user to resources on another server, use Response. Redirect.
· Use Response. Redirect to connect users to non-ASPX resources, such as HTML pages.
· If you want to retain the query string as part of the URL, use Response. Redirect.
· If you want to transfer the execution process to another ASPX page of the same Web Server, you should use Server. transfer instead of Response. redirect, because Server. transfer can avoid unnecessary network communication to achieve better performance and browsing performance.
· If you want to capture the output result of An ASPX page and insert the result to a specific location of another ASPX page, use Server. Execute.
· To ensure that the HTML output is valid, use Response. Redirect instead of Server. Transfer or Server. Execute.

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.