Pages

Friday, November 11, 2011

How to go back to the previous page in ASP.NET 2.0


There are various ways using which you can navigate back to the previous page. To keep the example short and simple, I will be using two pages and a few buttons to demonstrate the navigation. So let us get started:
Step 1: Create an ASP.NET Project with two pages, Page1.aspx and Page2.aspx.
Step 2: On Page1.aspx, drag and drop a button control. In the click event, use this code:
C#
protected void Button1_Click(object sender, EventArgs e)
{
     Response.Redirect("Page2.aspx");
}
VB.NET
Protected Sub Button1_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles Button1.Click
        Response.Redirect("Page2.aspx")
End Sub
Step 3: On Page2.aspx, we will be dragging and dropping 3 buttons. Each button will represent a method to go back to the previous page. Let us explore them one at a time:
Method 1 – Using a static variable and UrlReferrer (only for understanding, not to be used in production apps)
URLReferrer gets the URL of the previous page that linked to the current URL. To use this property, declare a static variable called ‘prevPage’ in Page2.aspx. Drag and drop a button, button1 on Page2.aspx. On the Page_Load, use the Request.UrlReferrer to populate the prevPage variable with its value. Then on the button1 click event, use this variable to go back to the previous page as demonstrated below :
C#
// static variable
static string prevPage = String.Empty;
 
protected void Page_Load(object sender, EventArgs e)
{
     if( !IsPostBack )
     {
         prevPage = Request.UrlReferrer.ToString();
     }
 
 }
 
 protected void Button1_Click(object sender, EventArgs e)
 {
      Response.Redirect(prevPage);
 }
VB.NET
'static variable
Private Shared prevPage As String = String.Empty
 
Protected Sub Page_Load(ByVal sender As ObjectByVal e As EventArgs) Handles Me.Load
        If (Not IsPostBack) Then
            prevPage = Request.UrlReferrer.ToString()
        End If
End Sub
 
Protected Sub Button1_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles Button1.Click
        Response.Redirect(prevPage)
End Sub
 
Method 2 – Using Javascript
Drag and drop another button called button2 on the Page2.aspx. In the Page_Load event, add the following lines of code :
C#
protected void Page_Load(object sender, EventArgs e)
{
Button2.Attributes.Add("onClick""javascript:history.back(); return false;");
}
 
protected void Button2_Click(object sender, EventArgs e)
{
      
}
 
VB.NET
 
Protected Sub Page_Load(ByVal sender As ObjectByVal e As EventArgs) Handles Me.Load
Button2.Attributes.Add("onClick""javascript:history.back(); return false;")
End Sub
 
Protected Sub Button2_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles Button2.Click
 
End Sub
 
Note : Notice the ‘return false’ snippet used in the Button2.Attributes.Add method. Well this is used to cancel the submit behaviour that occurs on the button click. Since the Click event precedes over the other events, we need to return false to cancel the submit and go back to the previous page.
 
 
Method 3 – Using ViewState
 
If you do not intend to declare a static variable (which you should avoid in all cases), you can use viewstate to go back to the previous page by using the same UrlReferrer property that we used in Method 1. To do so, drag and drop a third button, Button3 on Page2.aspx. In the Page_Load event, use the ViewState to store the value of the Request.UrlReferrer property. Then access the same value in the click event of the third button to go back to the previous page as shown below:
C#
protected void Page_Load(object sender, EventArgs e)
{
     if( !IsPostBack )
     {
         ViewState["RefUrl"] = Request.UrlReferrer.ToString();
     }
 
 }
 
protected void Button3_Click(object sender, EventArgs e)
{
     object refUrl = ViewState["RefUrl"];
     if (refUrl != null)
         Response.Redirect((string)refUrl);
}
 
VB.NET
 
 
Protected Sub Page_Load(ByVal sender As ObjectByVal e As EventArgs) Handles Me.Load
        If (Not IsPostBack) Then
            ViewState("RefUrl") = Request.UrlReferrer.ToString()
        End If
End Sub
 
 
Protected Sub Button3_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles Button3.Click
      Dim refUrl As Object = ViewState("RefUrl")
      If Not refUrl Is Nothing Then
          Response.Redirect(CStr(refUrl))
      End If
End Sub
 
 
Well as we saw, it was quiet easy to use these different methods to go back to the previous page. You are free to experiment and use any of these methods (avoid static variables though). If you have experimented with any method, you can share it over here with other readers.
I hope this article was useful and I thank you for viewing it. Happy Coding!! 

No comments:

Post a Comment