Home > ASP.Net > How to go back to the previous page in ASP.NET

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


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:

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

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
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 :

// 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);
 }

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 :

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)
{

}

Method 3 – Using ViewState
 
If you do not intend to declare a static variable, 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:

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);
}

 

Good Luck

Advertisements
Categories: ASP.Net
  1. Shiayi
    June 13, 2009 at 9:54 pm

    The code work. Thanks.

  2. Priyanka Mathur
    September 2, 2009 at 12:09 pm

    Hi, this post realy help me alot
    I am trying looking many website but will not able to figure it out. how to go back to the previous page using javascript, But finaly I able to find out that have to pass return false on the button onclick event i.e.
    onclick =”javascript:history.back();return false;”

  3. yasserzaid
    September 2, 2009 at 7:23 pm

    Thanks both Shiayi , Priyanka Mathur for you comment hope this helps you

  4. June 20, 2011 at 12:26 pm

    Thanks Dude………
    The given code works Smoothly

    Happy Coding …
    🙂

    • yasserzaid
      May 23, 2012 at 9:33 am

      @ shikhar : you are welcome hope my post helps you

  5. Hari
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: