Redirecting Users to Another Page

JavaScript Editor jscript editor     Web designer 



Main Page

You will often want to redirect users to other pages as part of your Web application. ASP.NET provides the following ways for you to build redirection into your Web pages:

Each of these options is described below. A table at the end of the topic summarizes the options and provides guidelines to help you decide when to use each option.

Hyperlinks

You can use an HTML anchor tag (<a>) on an ASP.NET Web page to create static links, or you can programmatically control the link text and target URL of hyperlinks by using the HyperLink control. In this scenario, the user explicitly clicks a link and the browser transfers to the target page. The target page is invoked using an HTTP GET command. Therefore, no information about the source page is passed to the target page unless you specify a query string on the URL of the target page. If the source and target page are in the same Web application, they can share information using session state or application state.

Cross-Page Posting

By default, buttons in an ASP.NET Web page post the page to itself. Cross-page posting enables you to configure a button on an ASP.NET Web page to post the current page to a different page. A typical example is when creating a multi-page form. You can configure buttons on the page to move to the next and previous pages of the form.

Cross-page posting is similar to hyperlinks in that the transfer is initiated by a user action. However, in cross-page posting, the target page is invoked using an HTTP POST command, which sends the values of controls on the source page to the target page. In addition, if the source and target page are in the same Web application, the target page can access public properties of the source page. As always, all of the pages in the application can share information stored in session state or application state.

For more information, see Cross-Page Posting in ASP.NET Web Pages and How to: Post ASP.NET Web Pages to a Different Page.

Redirecting Programmatically Using the Browser

You can programmatically redirect (that is, force the browser to a new page without user intervention) by calling the Redirect method. The method sends a command to the user's browser that causes the browser to get the target page. Calling the Redirect method is the programmatic equivalent of clicking a hyperlink in that it results in a new request (an HTTP GET command) for the target page. Because you are calling the Redirect method from your own code, you can dynamically define the target URL, including any query-string information, based on your application requirements. As with hyperlinks, the Redirect method does not pass post information to the target page. If the source and target pages are in the same Web application, you can share data between the source and target pages by storing it in session state.

Redirecting Programmatically on the Server

You can also redirect programmatically to a target page on the server by calling the Transfer method. In this scenario, the server simply transfers the current source page context to the target page. The target page then renders in place of the source page. The source and target pages must be in the same Web application. As with cross-page posting, the Transfer method has the advantage that it allows the source page to read control values and public property values from the source page.

Because the transfer between source and target pages happens on the server, the browser has no information about the changed page, and it retains information about the original (source) URL. For example, the Address box in Internet Explorer does not change after a transfer, and instead continues to show the URL of the page it most recently requested (which is usually the source page). The browser's history is not updated to reflect the transfer. This can result in unexpected behavior if the user refreshes the page in the browser or clicks the browser's back button. Therefore, calling the Transfer method is a strategy that is best used in applications where you are presenting pages to the user with the URL hidden.

Selecting a Redirect Option

The following table summarizes the possible ways to redirect between pages.

Strategy Characteristics Usage

Hyperlinks

  • Performs new request on the target page.

  • Does not pass current page information to the target page.

  • Requires user initiation.

  • Redirects to any page, not just pages in the same Web application.

  • Enables you to share information between pages by using query string or session state. (The HyperLink control allows you to create URL and query strings programmatically.)

  • For navigation without any additional processing, as in menus or lists of links.

  • When navigating to another page should be under user control.

Cross-page posting

  • Posts current page information to the target page.

  • Makes post information available in the target page.

  • Requires user initiation.

  • Redirects to any page, not just pages in the same Web application.

  • Allows the target page to read public properties of the source page when these pages are in the same Web application.

  • To pass current page information to the target page (as in multi-page forms).

  • When navigation should be under user control.

Browser redirect

  • Performs a new request on the target page.

  • Passes query string to the target page.

  • Provides programmatic and dynamic control over the target URL and query string.

  • Enables you to redirect to any page, not just pages in the same Web application.

  • Enables you to store information from the source page in session state before redirecting to share with the target page.

  • For conditional navigation, when you want to control the target URL and when the navigation takes place. For example, use this option if the application must determine which page to navigate to based on data provided by the user.

Server transfer

  • Transfers control to a new page that renders in place of the source page.

  • Redirects only to target pages that are in the same Web application as the source page.

  • Enables you to read values and public properties from source page.

  • Does not update browser information with information about the target page. Pressing the refresh or back buttons in the browser can result in unexpected behavior.

  • For conditional navigation, when you want to control when the navigation takes place and you want access to the context of the source page.

  • This option is best used in situations where the URL is hidden from the user.

See Also



JavaScript Editor jscript editor     Web designer