Navigation Between Pages






Navigation Between Pages

A typical Web application is a collection of Web pages linked to each other. You can use the HyperLink control to navigate to a different Web page when the hyperlink is clicked. If you need to navigate to a Web page programmatically, you can use the following methods provided by ASP.NET:

  • Response.Redirect()

  • Server.Transfer()

  • Server.Execute()

The Response.Redirect() Method

The Response.Redirect() method causes the browser to connect to the specified URL. When the Response.Redirect() method is called, it creates a response whose header contains a 302 (Object Moved) status code and the target URL. When the browser receives this response from the server, it uses the header information to generate another request to the specified URL. When using the Response.Redirect() method, the redirection occurs at the client side and involves two round trips to the server.

Using the Response.Redirect() method is recommended in the following cases:

  • You want to connect to a resource on any Web server.

  • You want to connect to a non-ASPX resource (such as an HTML file).

  • You want to pass a query string as part of the URL.

The Server.Transfer() Method

The Server.Transfer() method transfers the execution from the current ASPX page to the specified ASPX page. The path specified to the ASPX page must be on the same Web server and must not contain a query string.

When this transfer occurs, execution of the current ASPX page terminates and control is transferred to another ASPX page. The new ASPX page still uses the response stream created by the prior ASPX page. The URL in the browser still shows the original page because the redirection occurs on the server side and the browser remains unaware of the transfer.

When you want to transfer control to an ASPX page residing on the same Web server, you should use Server.Transfer() instead of Response.Redirect() because Server.Transfer() avoids an unnecessary round trip and provides better performance and user experience.

The default use of the Server.Transfer() method does not pass the form data and the query string of the original page request to the page receiving the transfer. However, you can preserve the form data and query string of the original page by passing a true value to the optional second argument of the Server.Transfer() method. The second argument takes a Boolean value that indicates whether to preserve the form and query string collections.

graphics/note_icon.gif

In the Server.Transfer() method, when you choose to preserve the form and query string collections, you need to be aware of one thing: The destination page contains the form and query string collections that were created by the original page. As a result, the hidden __VIEWSTATE field of the original page is also preserved in the form collection. The view state is page scoped and is valid for a particular page only, which causes the ASP.NET MAC to announce that the view state of the new page has been tampered with. Therefore, when you choose to preserve the form and query string collections of the original page, you must set the EnableViewStateMac attribute of the Page directive to false for the destination page.


The Server.Execute() Method

The Server.Execute() method enables the current ASPX page to execute a specified ASPX page. However, the path to the specified ASPX page must be on the same Web server and must not contain a query string.

After the specified ASPX page is executed, control transfers back to the original page from which the Server.Execute() method was called. This technique of page navigation is analogous to making a method call to an ASPX page.

The called ASPX page has access to the form and query string collections of the calling page. Thus, for the reasons explained in the previous section, you must set the EnableViewStateMac attribute of the Page directive to false on the called ASPX page.

By default, the output of the executed page is added to the current response stream. This method also has an overloaded version in which the output of the redirected page can be fetched in a TextWriter object instead of adding the output to the response stream. This helps you control where the output is placed on the original page.

graphics/note_icon.gif

The output returned to the browser by Server.Execute() and Server.Transfer() might contain multiple <html> and <body> tags because the response stream remains the same while executing another ASPX page. Therefore, the output that results from calling these methods might contain invalid HTML code.



     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows