Link to home
Start Free TrialLog in
Avatar of SoutTran
SoutTran

asked on

Cancel button to go back to previous page

Hi Everyone,

I tried using the below javascripts on my vb code behind for both the page_load and btCancel_Click event, it does not take me back to the previous page.

  btCancel.Attributes.Add("onClick", "javascript:history.back();")
OR
btCancel.Attributes.Add("onClick", "javascript:history.go(-1);")

What else can I try?????

The Cancel button is part of a form to gather user input data and submit them if users desired to but they want to cancel out this page, they can click on the cancel button to go back to the previous page where they come from.

Thanks in advance
Avatar of praneetha
praneetha

<A onmouseover="ibtnCancel.src='Buttons/canceldown.png';" onmouseout="ibtnCancel.src='Buttons/cancelup.png';"
                                                href="javascript:history.back()"><IMG src="buttons\cancelup.png" border="0" name="ibtnCancel"></A>
                                    

instead of asp.net button you can just have html button or <a link as shown above...

ASKER CERTIFIED SOLUTION
Avatar of praneetha
praneetha

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SoutTran

ASKER

I was using the HTML button before, but since I need to reference this control in the codebehind it gave me error with regards to Object not instantiated. So I tried to go back using the below HTML button, it works. I don't know why it didn't worked before. I'll need to do some more testing.

<input id="btCancel" onclick="Javascript:history.back();" type="button" value="Cancel"                  name="btCancel">

Anyway, thanks so much!
ASP.NET buttons (in the System.Web.UI.Webcontrols family) are ALL automatically SUBMIT buttons.  That means when you click them, they submit the page.  This action takes precedence over any onclick events that you code.  However, you can cancel the submit by returning a value of false from YOUR onclick code.

So your original idea would work, but you need to add a return false.

  btCancel.Attributes.Add("onClick", "javascript:history.back(); return false;")