0Day Forums
Hyperlink to go back to previous page in asp .net - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Asp.Net (https://0day.red/Forum-Asp-Net)
+--- Thread: Hyperlink to go back to previous page in asp .net (/Thread-Hyperlink-to-go-back-to-previous-page-in-asp-net)

Pages: 1 2


Hyperlink to go back to previous page in asp .net - reparations985725 - 07-23-2023

I have a page in asp .net `(

[To see links please register here]

).`

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


RE: Hyperlink to go back to previous page in asp .net - ansermet526 - 07-23-2023

you can use this:

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


RE: Hyperlink to go back to previous page in asp .net - Sirrugg7 - 07-23-2023

use this code


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

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

</body>
</html>


RE: Hyperlink to go back to previous page in asp .net - Prodericax - 07-23-2023

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.


RE: Hyperlink to go back to previous page in asp .net - malease396975 - 07-23-2023

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


RE: Hyperlink to go back to previous page in asp .net - leatherneck16 - 07-23-2023

I found this example

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

Just put this your page, it is working.


RE: Hyperlink to go back to previous page in asp .net - holmen596 - 07-23-2023

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>







RE: Hyperlink to go back to previous page in asp .net - squirarchies540929 - 07-23-2023

If you are using asp.net then remember that

javascript:history.go(-1)
and

window.history.back()

Both will take you to back page. <br/>
But the previous page will not be exactly previous page.<br/>
For example
---
Suppose you are on page `Default.aspx` and there is a `asp:button`<br/>
Now when you click on the button and you are back on `Default.aspx`<br/>
**in this situation your previous page is still you Default.aspx**

**Take another exapmle<br/>**
You have two pages `Default1.aspx` and `Default2.aspx`
<br/>
**Condition 1**:- button clicked on `Default1.aspx` which redirect you to `Default2.aspx`<br/>
ok your previous page is `Default1.aspx`
<br/>
**Condition 2**:- button clicked on `Default1.aspx` and post back on the same `Default1.aspx` page
<br/> 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);
}



RE: Hyperlink to go back to previous page in asp .net - modelists940705 - 07-23-2023

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.



RE: Hyperlink to go back to previous page in asp .net - abad997 - 07-23-2023

In your C# code just write this:

Response.Write("<script>history.go(-1);</script>");