Several methods of page navigation and comparison sharing _ practical skills

Source: Internet
Author: User
One, Super link
The easiest way to get to another form from one form is to use the HTML Hyperlink control. In Web Forms, HTML code classes that use hyperlinks are as follows:
Copy Code code as follows:

<a href= "webform2.aspx" > Entry Form 2</a>

When the user clicks on the hyperlink, Webform2.aspx executes and sends the results to the browser. Hyperlink navigation can be used almost anywhere, including HTML pages and ordinary ASP pages. Asp. NET also provides another alternative use, the Hyperlink server control:
Copy Code code as follows:

<form id= "Form1" method= "POST" runat= "Server" >
<asp:hyperlink id= "HyperLink1" runat= "Server"
Navigateurl= "webform2.aspx" > Entry Form 2</asp:hyperlink>

The results of the above HTML code are the same as the first example, because treats the Hyperlink Web server control as an HTML hyperlink control. But there is a significant difference between the two, HyperLink Web server controls can be programmed on the server side. Specifically, you can change its NavigateUrl properties in your program code, allowing you to construct hyperlinks that can dynamically change depending on the current state of the application, such as:
Copy Code code as follows:

Private Sub Button1_Click (_
ByVal sender as System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Hyperlink1.navigateurl = "Webform3.aspx"
End Sub

After this code executes, if the user clicks on the link, he sees the webform3.aspx, not the webform2.aspx.
second, using program control redirect
Although hyperlinks can be navigated from one page to another, this navigation is entirely user-controlled. Sometimes, we might want to use code to control the entire navigation process, including when to go to another page. On these occasions, ASP. NET has three different ways to achieve similar goals: Call the redirect method of the Response object, and invoke the transfer or Execute method of the server object. The behavior of these three types of navigation is basically 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 invoked, it creates an answer that States Code 302 (indicating that the target has changed) and a new target URL is indicated in the reply header. The browser receives the response from the server and uses the information in the answer header to issue a request to the new URL.
Say When using the Response.Redirect method, the redirection occurs on the client, involving two communications with the server (two back and forth): the first is a request to the original page, get a 302 answer, the second is the request 302 reply to the new page, get redirected after the page.
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 invoked, the current ASPX page terminates execution, and the execution process is transferred to another ASPX page, but the new ASPX page still uses the reply stream created by the previous ASPX page.
If you use the Server.Transfer method to navigate between pages, the URL in the browser does not change, because the redirection is done entirely on the server side, and the browser has no idea that the server has performed a page transformation.
By default, the Server.Transfer method does not pass the form data or query string from one page to another, but you can keep the form data and query string for the first page as long as the second argument of the method is set to true.
At the same time, use Server.Transfer should be noted that the target page will use the original page created by the response stream, which led to machine verification check (Machine authentication check, MAC) that the viewstate of the new page has been tampered with. Therefore, if you want to preserve the form data and the collection of query strings for the original page, you must set the enableViewStateMac property of the page directive for the target pages to false.
2.3 Server.Execute
The Server.Execute method allows the current ASPX page to execute a specified ASPX page on the same Web server, and when the specified ASPX page finishes executing, the control process returns to the original page where the Server.Execute call was made.
This kind of page navigation is similar to a function call to an ASPX page, where the called page has access to the form data and the collection of query strings that emit the calling page, so set the enableViewStateMac property of the page instruction for the call to False.
By default, the output of the called page is appended to the current reply stream. But The Server.Execute method has an overloaded method that allows you to get the output of the called page through a TextWriter object (or its child objects, such as the StringWriter object) instead of appending directly to the output stream, so that you can easily adjust the location of the output of the called page in the original page.
To illustrate its working process, let's create a Web form, put a button control (Button1) and a text control (LITERAL1), and move into code view in the design interface, Add an Imports statement with a System.IO namespace, and then add the code to execute when the user clicks the button:
Copy Code code as follows:

Private Sub Button1_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

Then create a second page webform2.aspx for the same Web application. To the HTML view of the page, modify its page directive to prohibit viewstate checking:
Copy Code code as follows:

<%@ Page language= "vb" autoeventwireup= "false" codebehind= "WebForm2.aspx.vb"
inherits= "Navigate.webform2" enableviewstatemac= "false"%>

Go to Design view and add some controls to the second page. Next, set the first page as the default page and start the application. Click the button and the WebForm2 control will display where the literal button is placed in the WEBFORM1, noting that the page title and URL still show the original page WebForm1.
When navigating with Server.Transfer or Server.Execute methods, note that the resulting page may not be a legitimate HTML page because the page that is eventually returned to the client may contain multiple <HTML> and <body > and other markings. IE browsers seem to tolerate and handle such situations correctly, but if users want to use other browsers, it's best to test them carefully.
Iii. comparison and selection
Since there are so many ways to navigate from one page to another, how do you choose the best way to navigate? Here are some factors to consider:
• Hyperlinks are most appropriate if you want to let users decide when to convert the page and which page to go to.
• If you want to use a program to control the target of a transformation, the timing of the conversion is determined by the user, using the Web server's hyperlink control to dynamically set its NavigateUrl property.
• Use Response.Redirect If you want to connect a user to a resource on another server.
• Use Response.Redirect to connect users to non-ASPX resources, such as HTML pages.
• Use Response.Redirect If you want to keep the query string as part of the URL.
• If you want to transfer the execution process to another ASPX page on the same Web server, You should use Server.Transfer instead of Response.Redirect, because Server.Transfer can avoid unnecessary network traffic, resulting in better performance and browsing effects.
• Use Server.Execute If you want to capture the output of an ASPX page and then insert the result into a specific location in another ASPX page.
• If you want to make sure that the HTML output is legitimate, use Response.Redirect and do not use the Server.Transfer or Server.Execute methods.
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: 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.