1

I have a asp.net page where it has the following properties

<asp:Button ID="btnBack" runat="server" CssClass="Button" Text="< Back to Home" /></td>

And in the C# code file I am doing something like this

btnBack.Attributes.Add("onclick", "javascript:history.go(-1);return false");

The problem is it is not always returning me to the Home page but some other page..

Is there anyway I can add in this function the page where I want to navigate for instance (and I know its wrong but please see what i want to achieve)

    btnBack.Attributes.Add("onclick", "~/Home.aspx");

Something like that

I would appreciate your help

3 Answers 3

1

The reason you aren't always getting the behavior of moving back to the home page is because history.go(-1) just tells the browser to navigate back to the previous page that was loaded. It may not always be the home page.

A simple solution from javascript is to change the location property which will trigger the broswer to load the URL supplied

btnBack.OnClientClick = "window.location='http://www.google.com'";

The OnClientClick will attach the javascript string to the onclick event in html markup for you. This is easier and cleaner IMO than something like btnBack.Attributes.Add("onclick", "window.location=~/Home.aspx"

Sign up to request clarification or add additional context in comments.

Comments

0
btnBack.Attributes.Add("onclick", "location='http://google.com'");

1 Comment

location is an object; you are looking for location.href, which is a string.
0
btnBack.Attributes.Add("OnClick", "javascript:window.location.reload('Home.aspx');return false;")

1 Comment

Please do not add javascript: to event attributes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.