Navigation Techniques in ASP.Net

Introduction

This article provides a very basic understanding of navigation in a web application but plays a very important role in making an application perform well and to use good navigation techniques.

Guys, navigation in a web application is very important to understand since practically several times in a web application project we need to navigate from one page to another and return to the page of the original page as part of the functionality.

Navigation can cause data loss if it not properly handled. We do have many techniques to transfer data from one page to another but every technique has its own importance and benefits.

We will discuss the following techniques in this article.

  • Response.Redirect
  • Server.Transfer
  • Server.Exceute
  • Cross page posting
Response.Redirect

This is one of the navigation techniques in ASP.NET to move from one web form to another.

Guys, MSDN says it as:

It redirects a client to a new URL. Specifies the new URL and whether execution of the current page should terminate.
  1. Response.Redirect("WebForm1.aspx");  
response redirect

Processing

When processing the client request when a web server encounters the code block response.redirect with the location URL to be redirected it will send back a response header to the client then the client initiates a get request of the new page.

In this example we can see an extra round-trip to the web server being done when a response.redirect is used.

processing

We can navigate to another URL using response.redirect either on the same server or a different server. Like in the following example we can go to the Google home page hosted on a different server using this technique. 
  1. Response.Redirect("https://www.google.co.in/?gfe_rd=cr&ei=BpszVKXmIsPN8gfBi4GABg&gws_rd=ssl");  
It also maintains the history of an URL from where it has got navigated to, meaning we can return using the back button.

Important Sticky
  • It redirects to a new redirected URL where it is redirected in the browser.
  • It maintains the history and previous page is available with the back button.
  • It redirects the user to a web page hosted on the same server or a different server.
  • It has an additional round trip to the server that makes it a bit slower.

Server. Transfer

It is a navigation technique in an ASP.NET web application to move from one web form to another on the same server without changing the URL in an address bar.

This is the syntax for using this technique, basically it expects the URL of where you want to navigate to.

  1. Server.Transfer("Webform2.aspx");  
server dot transfer

On Webform2.aspx page load.

The namespace System.Collections.Specialized contains NameValueCollection.
  1. System.Collections.Specialized.NameValueCollection formVals = Request.Form;  
Sample Code
  1. namespace NevigationTest  
  2. {  
  3.       public partial class WebForm2 : System.Web.UI.Page  
  4.       {  
  5.             protected void Page_Load(object sender, EventArgs e)  
  6.             {  
  7.                   System.Collections.Specialized.NameValueCollection formVals = Request.Form;  
  8.   
  9.                   Label1.Text = formVals["ctl00$MainContent$txtName"];  
  10.                   Label2.Text = formVals["ctl00$MainContent$TextBox2"];  
  11.                   Label3.Text = formVals["ctl00$MainContent$TextBox3"];  
  12.   
  13.             }  
  14.       }  
  15. }
sample code

Post information from ASP.NET Web pages including the values of hidden fields, such as __VIEWSTATE, __EVENTTARGET and __EVENTARGUMENT, that are used for internal processing in the page.

You can either eliminate these by coding or hardcoding the controls that you want to use it in to get the values from the source page.

Using Previous Page

Using the PreviousPage property you will able to find the control as in the following:
  1. Page Pg = Page.PreviousPage;  
Sample Code
  1. namespace NevigationTest  
  2. {  
  3.       public partial class WebForm2 : System.Web.UI.Page  
  4.       {  
  5.             protected void Page_Load(object sender, EventArgs e)  
  6.             {  
  7.                   Page Pg = Page.PreviousPage;  
  8.                   if(Pg != null)  
  9.                   {  
  10.                         Control placeHolder = Pg.Controls[0].FindControl("MainContent");  
  11.                         TextBox prevTxtBox =  (TextBox)placeHolder.FindControl("txtName");  
  12.   
  13.                         if (prevTxtBox != null)  
  14.                         {  
  15.                               Label1.Text = prevTxtBox.Text;  
  16.                         }  
  17.                    }  
  18.             }  
  19.       }  
  20. }  
Redirection to URL to another Webserver

You can navigate to websites on another web sever using the server.transfer method but it will supply a run time exception. Using the following code I am trying to navigate to the Google home page:
  1. protected void Button4_Click(object sender, EventArgs e)  
  2. {  
  3.       Server.Transfer("https://www.google.co.in/?gfe_rd=cr&ei=BpszVKXmIsPN8gfBi4GABg&gws_rd=ssl");  
  4. }  
argumentexception

Important Sticky

 

  • It transfers a current page request to another .aspx page on the same server.
  • It does not change the address bar.
  • It preserves server resources and avoids the unnecessary roundtrips to the server.
  • It cannot maintain the history.
  • There is no URL changes in the address bar in this case so the back button cannot be used.
Server.Exceute

It is also a navigation technique similar to server.transfer but has a different behavior when doing the process.
  1. Server.Execute("WebForm3.aspx");  
server execute

When using the server.Exceute method for navigation it helps to retain the execution control from the source web form.

In the above example default.aspx is a source web form from where we have navigated to webform3.aspx using server.exceute. When the control finds the code block of navigation it moved the control and starts processing the webform3.aspx but does not leave the control from default.aspx and after completion of the target web form control again move to the source web form and execute the further code processing on default.aspx page.

Important Sticky

It is very similar to the server.execute method for navigation but it retains the control from the source web page and returns to the original page after execution of the target page.

Cross Page posting

In an ASP.NET web form is the default page posted on itself whenever a button is clicked.

The Cross Page posting technique allows a web form to post on another web form on button click. The PostbackUrl property of the button is set to the page where you want to do cross-page posting.

Here is an example of what we are doing for webform4.

select url

Sample Code


The following code shows how to retrieve values from the source page and display it to the target page after cross-page posting on a button click from a previous page.
  1. namespace NevigationTest  
  2. {  
  3.       public partial class WebForm4 : System.Web.UI.Page  
  4.       {  
  5.             protected void Page_Load(object sender, EventArgs e)  
  6.             {  
  7.                   Page pg = Page.PreviousPage;  
  8.   
  9.                   if (pg != null)  
  10.                   {  
  11.                         Control placeHolder =  pg.Controls[0].FindControl("MainContent");  
  12.                         TextBox prevTxtBox =  (TextBox)placeHolder.FindControl("txtName");  
  13.   
  14.                         if (prevTxtBox != null)  
  15.                         {  
  16.                               Label1.Text = prevTxtBox.Text;  
  17.                         }  
  18.                   }  
  19.             }  
  20.       }  
  21. }  
Conclusion

We have a number of navigation techniques available with ASP.NET, it is very important to understand which techniques is required for your application.

Apply the right technique when coding to enhance application performance and avoid making resource waste and server round trips. In some cases the application breaks down.

Keep Smiling ….

MSDN References:

HttpResponse.Redirect Method (String, Boolean)

Up Next
    Ebook Download
    View all
    Learn
    View all