472,805 Members | 1,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 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 4711
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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.