473,322 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 4746
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: | last post by:
Hi, I am setting the NumericUpDown .Value property and the ValueChanged event is NOT being fired. Does this ONLY get fired when I change it on the UI and not programatically? Thanks
3
by: Robert | last post by:
I need some assistance doing some "right way to do it" coding. The following are EventHandlers associated with Delegates in a child form that call a procedure in the MDI form that resets a timer....
4
by: Anatoly | last post by:
Put any control on web page. create Init event for ths control. Write Response.Write("here") inside this event. Compile\build\run. I never saw "here" string appear on web page. Why???
3
by: RSB | last post by:
Hi every one , i am creating this form and i have two asp:Linkbuttons one for save and one for Cancel. Now i also have some RequiredFieldValidators on this Form and i only want to Execute...
3
by: Armin | last post by:
Hello I have a UserControl with a Click Event. Is it possible to find out the List of all Delegates/Eventhandlers using the Event. I read something about a "getinvocationlist" Methode for...
2
by: Simon Verona | last post by:
I have a usercontrol with code in the "leave" event which updates the final data back into a database. This works fine except if I have a default "accept" button on a form and invoke it by...
1
by: anat | last post by:
I want to show a confirm message before the gridview delete a record in the event: of deleting. I put the following code and it shows the message twice. why? rotected void...
0
by: Artur | last post by:
After trying to solve this, I noticed another strange thing. When in secure connection Page_Load method of page is not invoked (Load event not fired). I looked where this method is assigned to this...
2
by: APA | last post by:
Why does adding code to the form submit function using the RegisterOnSubmitStatement method prevent the server side event handler for the submit button from firing? This is completely useless. I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.