When using RegisterOnSubmitStatement eventhandlers are not fired

A

APA

Well, I've figured out a way around this mess. I have no idea why it doesn't work the way I think it should but I do know how to get it to work. The
scenario is that I have a form that has one submit button on it. I want a client side validation function to run before submit. I have a server side
event handler on the button that executes the necessary server side code. Sounds simple. Well, the problem is that once you put client side script
in the onsubmit event of the form either by entering it directly into the <form> tag (i.e. <form runat="server" onsumbit="myfunction();"> or by using
the RegisterOnSubmitStatement method to specify the script it will no longer fire the button's event handler.

The fix is to do two things (not counting not using the form onsubmit event). First, set the button's UseSubmitBehavior attribute to true. This
forces the button to do the form submit by executing the __doPostBack function which properly posts back data that will get the .NET code to run the
button event handler. Second is to add some script that hijacks the __doPostBack function. Example below:


<script type="text/javascript" Language="JavaScript">
var netPostBack = __doPostBack;
__doPostBack = EscapeHtml;

function EscapeHtml(eventTarget, eventArgument){
var retVal = validateForm();
if(!retVal) return false;
return netPostBack (eventTarget, eventArgument);
}
</script>

This allows you to insert your own form validation function into the postback process. You need to use the
Page.ClientScript.RegisterClientScriptBlock method to do this so that it places the above code AFTER the definition of the __doPostBack function.

Again, I have no idea why the button's event handler is not fired when there is code in the form's onsubmit event (but it does when there isn't). So,
if anyone can shed some light on that it would sure be appreciated.


Enjoy.
 
G

Guest

Hi there,

In ASP.NET 2.0 they added two new properties, mentioned UseSubmitBehaviour
(which actually is invented to replace javascript postback with standard
browser form submit). There's another property which was invented for such
purpose - OnClientClick:

<asp:TextBox runat="server" ID="txt" />
<asp:Button runat="server" ID="btnSubmit" Text="Submit!"
OnClientClick="return validateForm();" />

<script type="text/javascript">
//<!--
function validateForm()
{
var txt = document.getElementById('<%=txt.ClientID %>');
return (txt.value != 'bullshit');
// remember function must return boolean
// value to prevent/allow postback
}
//-->
</script>

Please also note you could use standard validation controls to perform user
input validation, it's also possible to execute your own code when using
validation controls:

http://www.microsoft.com/communitie...pnet&mid=4ae1ffe7-2bcb-4307-b0a2-9656d5a770b4
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top