Hyperlink to go back to previous page in asp .net

85,481

Solution 1

you can use this:

<a href='javascript:history.go(-1)'>Go Back to Previous Page</a>

Solution 2

If you are using asp.net then remember that

javascript:history.go(-1)

and

window.history.back()

Both will take you to back page.
But the previous page will not be exactly previous page.

For example

Suppose you are on page Default.aspx and there is a asp:button
Now when you click on the button and you are back on Default.aspx
in this situation your previous page is still you Default.aspx

Take another exapmle
You have two pages Default1.aspx and Default2.aspx
Condition 1:- button clicked on Default1.aspx which redirect you to Default2.aspx
ok your previous page is Default1.aspx
Condition 2:- button clicked on Default1.aspx and post back on the same Default1.aspx page
Now your previous page is still Default1.aspx


Edit

 protected void Page_Load(object sender, EventArgs e)
 {
     if( !IsPostBack )
     {
        ViewState["RefUrl"] = Request.UrlReferrer.ToString();
     }
  }

and use this in back button as follows

 protected void Button3_Click(object sender, EventArgs e)
 {
      object refUrl = ViewState["RefUrl"];
      if (refUrl != null)
          Response.Redirect((string)refUrl);
 }

Solution 3

For Going to previous page

First Method

<a href="javascript: history.go(-1)">Go Back</a>

Second Method

<a href="##" onClick="history.go(-1); return false;">Go back</a> 

if we want to more than one step back then increase

For going 2 steps back history.go(-2)
For going 3 steps back history.go(-3)
For going 4 steps back history.go(-4)
and so on........

Solution 4

use this code

    <html>
    <head>
    <script>
    function goBack()
      {
      window.history.back()
      }
    </script>
    </head>
    <body>

    <a href="#" onclick="goBack()">Back</a>

    </body>
    </html>

Solution 5

I found this example

<input type="button" value=" &lt;-- BACK " onclick="history.go(-1);return false;">

Just put this your page, it is working.

Share:
85,481
user1400915
Author by

user1400915

Updated on February 06, 2020

Comments

  • user1400915
    user1400915 about 4 years

    I have a page in asp .net (http://localhost/error/pagenotfound).

    There is a link in page, on clicking on which has to go back to previous page from where I came from.

    <a href="#">Go Back to Previous Page.</a> 
    

    How can I go back to previous page by taking from history

  • user1400915
    user1400915 over 11 years
    Window.history.Back is going to the same landing page
  • user1400915
    user1400915 over 11 years
    Ya exactly any solution for this?
  • शेखर
    शेखर over 11 years
    Yes put your previous page name in the view-state. Means you will have to save it some where for your use.
  • शेखर
    शेखर over 11 years
    Edited my answer for your answer
  • Marzouk
    Marzouk over 7 years
    Thanks, really the answer that is signed as the correct answer depend on js browser APIs which have an issue like you describe so i suggest for any one to use this approach instead of the first one
  • Jeff Moretti
    Jeff Moretti over 4 years
    NOTE: I essentially did something 'similar' as above, where I did a history.go(-1) if zero postbacks where made (on the current page), and did a history.go(-2) if one or more postbacks where made on the current page (ie by the user clicking a button with an AJAX call, etc). And the logic to whether it was -1 or -2 was calculated in javascript (ie urlCountForBackButton = 2 was placed in the AJAX call success/error function)
  • nnunes10
    nnunes10 over 4 years
    This answer has some limitations. Imagine there is a sequence of pages P1 -> P2 -> P3. With this approach, you cannot go backwards from P3 to initial page P1.
  • शेखर
    शेखर over 4 years
    @nnunes10 If you want to do so you can add Array of URL in session and navigate accordingly. Above code is just to go to previous page. If you have that requirement add new question.