Page navigation in ASP.NET

Page navigation is the process of moving from one page to another page in your website. There are many ways to navigate from one page to another in ASP.NET.

  • Client-side navigation
  • Cross-page posting
  • Client-side browser redirect
  • Server-side transfer

1. Client-side navigation

Client-side navigation means that, your browser (client) or HTML request a web page by clicking on HyperLink. HyperLink control is the easiest way to navigate to another web page. It creates a link to another Web page. The HyperLink control renders an HTML anchor tag, <a>. The Text property is used for displaying the text in the HyperLink. We can also display an image on this control instead of text. To display an image we should set the ImageUrl property.

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Welcome.aspx">
         Goto Welcome Page
</asp:HyperLink>


When you see the view source on browser, you will get the rendered HTML as given below:

<a id="HyperLink1" href="Welcome.aspx">Goto Welcome Page</a>

When a user clicks on HyperLink control, browser called Welcome.aspx web page. You can also use JavaScript for client side navigation. You should use HTML input button for this. The document object represents the Web page in client-side JavaScript. You can call a particular web page, by providing the name of aspx page to its Document location property.

Let us take an example, in which we will take an HTML button. onclick event of HTML button called the client-side method name Button1_onclick.

Example

<input id="Button1" type="button" value="Goto Welcome Page"
           onclick="return Button1_onclick()" />


The JavaScript code for the Button1_onclick method is written into the <head> section of the aspx page as follows:

<script language="javascript" type="text/javascript">       
         function Button1_onclick()
           {
               document.location = "Welcome.aspx";
           }
</script>


When user clicks the button, Welcome.aspx page is called but no data is posted back to server.

2. Cross-page posting

By default, buttons and other controls that cause a postback on an ASP.NET Web page submit the page back to itself. ASP.Net provides a feature known as Cross Page PostBack that enables a web form to post-back to a different web form. A Button control supports PostBackUrl property that is used to set a Web page to which the processing occurs. The Page class has a property named PreviousPage that is used for reference of previous page. The previous page’s data is available in the Page.PreviousPage property.

Example:
Let us take an example in which there are two web pages named as Home.aspx and Welcome.aspx respectively. The first page contains a Button and a Textbox control. Set the PostBackUrl property of Button control as “~/Welcome.aspx”

// Sample code for Welcome.aspx.

protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.PreviousPage != null)
        {
            TextBox MyTextBox = (TextBox)Page.PreviousPage.FindControl("TextBox1");
            if (MyTextBox != null)
            {
                Label1.Text = MyTextBox.Text;
            }
        }
    }

3. Client-side browser redirect

The Page.Response object has redirect method. The Response.Redirect method redirects a request to a new URL. It instructs the browser to initiate a request for another Web page. The redirect is not a PostBack. It is similar to the user clicking a hyperlink on a Web page. The URL in the address bar of browser is updated according to parameter passed in Redirect method. The PreviousPage property does work when using the Redirect method. Response.Redirect takes an extra round trip to the server.

protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("TutorialRide.aspx");
    }

4. Server-side transfer

The Transfer method transfers all the state information in one ASP.NET Page to a second ASP.NET Page. Server.Transfer changes the page being rendered on the browser. This happens all on the server. A redirect is not issued to the web browser. The URL of address bar of browser is not changed in this process. Server.Transfer reduce server requests and keeps the URL the same. It avoids the round trip to server.

protected void Button1_Click(object sender, EventArgs e)
    {
        Server.Transfer("TutorialRide.aspx");
    }


Difference between Response.Redirect and Server.Transfer

Both Response.Redirect and Server.Transfer methods are used to navigate a user from one web page to another web page. But there are some differences between both the methods as follow.

Response.Redirect()Server.Transfer()
It sends you to a new page and update the address barIt does not update the address bar while transfer to new page
On browser you can click back.Back button of browser does not work
It takes additional round trips to the server on each request.It avoids the additional round trips to the serve.
You can pass the address of any web page as a parameter in Redirect methodThe requested web page should be on the same server.