Button in ASP, called in C# file via Javascript
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 apprecia开发者_JAVA技巧te your help
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"
btnBack.Attributes.Add("onclick", "location='http://google.com'");
btnBack.Attributes.Add("OnClick", "javascript:window.location.reload('Home.aspx');return false;")
精彩评论