469,934 Members | 1,540 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

When using RegisterOnSubmitStatement eventhandlers are not fired

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

Feb 22 '07 #1
1 4563
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/communities...2-9656d5a770b4

--
Milosz
"APA" wrote:
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 <formtag (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.

Feb 23 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Robert | last post: by
4 posts views Thread by Anatoly | last post: by
1 post views Thread by anat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.