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.
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.