Tuesday, August 10, 2010

Different ways of web page Redirect in Asp.Net

As a website developer many times we come across situation where we need to redirect pages, as there are many ways to redirect pages in asp.net and in HTML as well; one should know the difference between various web page redirect methods.
In this article we will discuss various web page redirecting methods we can use in asp.net

  1. Hyperlink: This is a traditional way of web page redirect which can use with HTML TAG i.e "A" tag. This is a static link on a page, which can be used as a tag with various style attributes. Also we can put data in opening and closing tag of "A". Asp.Net provides Hyper Link control, which is a class inherited from Webcontrol class. This control needs user to click explicitly on link for web page redirect.
    
     

    There are many attributes and events which are associated with hyperlink control, which can be used for many purposes.

    Hyperlink has following characteristics
    • New request is performed on Target Page.
    • Current page information doesn't get passed. Need to use query string to pass parameters
    • User Need to Initiate the web page transfer request.
    • Redirects to any page, not only restricted to current domain.
    When to Use
    • For navigation without any processing, e.g. Menu's list Items
    • When user should control the navigation.
  2. CrossPagePostBacks :By default, buttons in an ASP.NET Web page post the page to itself. i.e. it simply acts as a submit button of HTML. Where as Cross-page posting enables us to change the style of submitting the page, we can configure a button on an ASP.NET Web page to post the current page to a different page. Typically in multi-page forms, we can use such buttons on the page to move to the next and previous pages of the form.

    Though cross page posting is similar to hyperlinks, 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 source and target page are in the same web application then the target page can access public properties of the source page. You can check out the article on cross page post by, click on CROSS PAGE POSTBACK
    Hyperlink has following characteristics
    • Post current Page Information into Target Page.
    • Makes Post information available into target page.
    • User Need to Initiate the cross page postback request.
    • Redirects to any page, not only restricted to current domain.
    • Enables the target page to read public properties of the source page if the pages are in the same Web application.
    When to Use
    • To pass current page information to the target page (as in multi-page forms).
    • When user should control the navigation.
  3. Response.Redirect: This is simple HTTP redirection we can directly use in code behind. very simple to use  When we use Response.Redirect the browser issues a new request to the target server in the form of an HTTP GET request. Response.Redirect is same as form submit in HTML, it does exactly same, actually in HTML response.redirect come as submit only. This method passes the server elements using query string. We can always use this method in code behind with our logic to redirect to other page(s).
    Hyperlink has following characteristics



    • Performs a new HTTP GET request on the target page.
    • Passes the query string (if any) to the target page. In Internet Explorer, the size of the query string is limited to 2,048 characters.
    • 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 share information between source and target pages using session state.

    When to Use
    • For conditional navigation, when you want to control the target URL and control when 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.
  4. Server.Transfer: In case of Server.Transfer, server simple transfers the current source page context to the target page. The target page then renders in place of the source page. To use Server.Transfer, source page and target page should be in the same web application. When we use transfer method target page can read control values and public property values from the source page. Transfer between source and target pages happens on the server, because of this the browser has no information about the changes page, and it retains all information about the original i.e. source URL. Browser history doesn't get update to reflect the transfer. This is the best strategy to keep URL hidden, if user refresh the page he/she will be redirected to source page rather than new transferred page.
    Server.Transfer has following characteristics
    • Instead of source page control is transfered to new page which get renders in place of source
    • Redirects only to target pages that are in the same Web application as the source page.
    • Enables us to read values and public properties from source page.
    • Browser information Does not update with information about the target page. Pressing the refresh or back buttons in the browser can result in unexpected behavior.
    When to Use
    • For conditional navigation, when you want to control when navigation takes place and you want access to the context of the source page.
    • Best used in situations where the URL is hidden from the user.
Submit this story to DotNetKicks

0 comments: