Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript onsubmit event problem on asp.net form

Status
Not open for further replies.

EugeneBarnard

Programmer
Oct 3, 2006
1
ZA
Hi

I created an asp.net2 page similar to the one below that contains two or more buttons, eg a submit button (type=submit) and a cancel button (type=button). If I run the code from an asp.net 2 page, the cancel button incorrectly fires the ValidateForm before posting back to the server. The same code works when using asp.net 1.1, ie the submit button fires the ValidateForm method and the cancel button performs a postback without firing the onsubmit event.

As far as I know, the onsubmit javascript event should not be fired when the submit() method is called from code.

Can anyone tell me what I am doing wrong?

Thanks
Eugene

Code:
<html>
<head runat="server">
    <script>
		function ValidateForm(pfrm)
		{
			alert('Validate form event fired');
			return false;
		}
    </script>
</head>
<body>
    <form id="form1" runat="server" onsubmit="return ValidateForm(this);">
    
		<asp:TextBox ID="txtTextbox1" runat="server"></asp:TextBox> <br />
		<asp:TextBox ID="txtTextbox2" runat="server"></asp:TextBox> <br />
    
		<asp:Button ID="btnSubmit" runat="server" Text="submit" OnClick="btnSubmit_Click" />
		
		<input type="button" id="btnCancel" runat="server" value="cancel" onserverclick="btnCancel_ServerClick" />
    </form>
</body>
</html>
 
EugeneBarnard,

I would try setting the CausesValidation property of the cancel button to False.
 
Hello,

i am not sure, but doesn't the onsubmit need a submit button? (There are 3 types... submit, reset and button). Personally i would change the onsubmit to onclick.

Try it and tell us what happened!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top