Reputation: 1943
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
Upvotes: 24
Views: 93422
Reputation: 4953
In your C# code just write this:
Response.Write("<script>history.go(-1);</script>");
Upvotes: 0
Reputation: 11
Following the example of शेखर
I would suggest to do the following and avoid any cycle on the url while posting back.
string from = Request.UrlReferrer.ToString();
string here = Request.Url.AbsoluteUri.ToString();
if(from != here)
Session["page"] = Request.UrlReferrer.ToString();
On your button click
object refUrl = Session["page"];
if (refUrl != null)
Response.Redirect((string)refUrl);
This will save always the PAGE you are coming from.
Hope it helps.
Upvotes: 1
Reputation: 17614
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.
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);
}
Upvotes: 32
Reputation: 1
I've been looking at this all weekend and haven't found the answer I was looking for. The problem is, after a postback history.go(-1); will not go back to where you want to go - that is, the page that got you there (with any changes made).
Using Request.UrlReferrer.ToString(); will reload the page you are going back to and you may be like me and not want to lose any changes that were made to the page. Above Rizwan Gill in 2013 hit on the answer that you want to go back 2, 3 or 4 pages but he didn't give a good way to do it.
The best way to do this is actually to use a pop-up modal for your page when it is called if your are writing the calling page but if you want to do it this way and have a back button you'll have to remember each postback and count them.
So, start with your hidden field (in your .aspx file) :
<asp:hiddenfield id="fldPostbackCount"
value="0"
runat="server"/>
Then increment the value with each postback (in your .aspx.vb file):
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
fldPostbackCount.Value = 1
Else
fldPostbackCount.Value = Int32.Parse(fldPostbackCount.Value) + 1
End If
End Sub ' Page_Load()
(I'm sure the .cs people can handle the to C conversion on this)
Now back to your .aspx file you have to have a history button:
<a href="javascript:history.go(-<%: fldPostbackCount.Value %>)" title="Back to Previous Page">Back to Previous Page</a>
Upvotes: 0
Reputation: 51
I found this example
<input type="button" value=" <-- BACK " onclick="history.go(-1);return false;">
Just put this your page, it is working.
Upvotes: 5
Reputation: 2253
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........
Upvotes: 11
Reputation: 1398
You should add an attribute which is about onclick like below:
protected void Page_Load(object sender, EventArgs e)
{
yourButton.Attributes.Add("onClick", "javascript:history.back(); return false;");
}
protected void yourButtonClick(object sender, EventArgs e)
{
Response.Write("Stackoverflow <br/>");
}
This is about using a button to go previous page, you can modify it according to your changes.
Upvotes: 2
Reputation: 1046
use this code
<html>
<head>
<script>
function goBack()
{
window.history.back()
}
</script>
</head>
<body>
<a href="#" onclick="goBack()">Back</a>
</body>
</html>
Upvotes: 7
Reputation: 5912
you can use this:
<a href='javascript:history.go(-1)'>Go Back to Previous Page</a>
Upvotes: 41