I'm working on my ASP.NET web project using VS2010, C#, I'm inserting a hyperlink in my page, labeled as BACK, and I want it to act like browser back button, how should I implement it? what is the easiest way? how should I set its navigateURL property? thanks
6 Answers
<asp:button id="backButton" runat="server" text="Back"
OnClientClick="JavaScript:window.history.back(1);return false;"></asp:button>
Except that's a button, of course :-( (read the question, Steve)
Try
navigateurl="javascript:history.go(-1);"
-
thanks steve, but how can I have it in my hyperlink? how should I perform this javascript function using a hyperlink? Commented Oct 1, 2011 at 19:11
-
2Either use the edit I made in the navigateUrl attribute or use this:
<asp:Hyperlink Runat="server" NavigateUrl="#" onclick="javascript:history.go(1);return false;">BACK<asp:Hyperlink>
Commented Oct 1, 2011 at 19:19 -
thanks Steve, you saved me a great time! a short but fully helpful reply, have good luck Commented Oct 1, 2011 at 19:22
-
@SteveMorgan I was trying this but getting trouble in make this work properly, I have a series of screens to go back to not only 1 screen. Suppose I go like this A->B->C->D. Now I want back to work in reverse like D->C->B->A but it's working like, D->C->B->C->B and then it keeps on going round between B and C. Can you help please :( ..– yogiCommented Feb 14, 2015 at 14:19
You don't need ASP.NET, just use this HTML/JScript code:
<a href="javascript:history.go(-1)">Back</a>
@Steve Not sure how to comment on an existing answer but... I figured that I could just for kicks say that you could always make the "button" a "linkbutton":
If you are using JQuery, you can simply add data-rel="back"
to your anchor-tag
<a data-rel="back" data-role="button" data-icon="back">Back</a>
Try this..
private void btnBack_Click(object sender, System.EventArgs e)
{
string prevPage = Request.UrlReferrer.ToString();
Response.Redirect(prevPage);
}
or
<asp:button id="btnBack" runat="server" text="Back" xmlns:asp="#unknown">
OnClientClick="JavaScript: window.history.back(1); return false;"></asp:button>
-
You'll lost the state of the page if you do it from server side (using Response.Redirect). With Javascript though, the state is maintained– sohaibyCommented Jun 27, 2016 at 5:49
something like this should do
<button>
@Html.ActionLink("Back", "Index", "Students (your controller name here)", new { id = @Model.Student.Id })</button>
ANOTHER WAY
<a asp-action="Index" asp-controller="Students" asp-route-studentId="@Model.StduentId" class="btn btn-sm btn-success">Back to List</a>