473,836 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When using RegisterOnSubmi tStatement 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="myfun ction();"or by using
the RegisterOnSubmi tStatement 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 UseSubmitBehavi or 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="JavaS cript">
var netPostBack = __doPostBack;
__doPostBack = EscapeHtml;

function EscapeHtml(even tTarget, 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.ClientScri pt.RegisterClie ntScriptBlock 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 4774
Hi there,

In ASP.NET 2.0 they added two new properties, mentioned UseSubmitBehavi our
(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.getEle mentById('<%=tx t.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="myfun ction();"or by using
the RegisterOnSubmi tStatement 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 UseSubmitBehavi or 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="JavaS cript">
var netPostBack = __doPostBack;
__doPostBack = EscapeHtml;

function EscapeHtml(even tTarget, 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.ClientScri pt.RegisterClie ntScriptBlock 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
9930
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
1887
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. There must be a better way to code this than calling OnResetTimer in all these procedures separately. Is there not a way to use one handler for all of them? private void pnlCPI_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)...
4
3910
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
2233
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 those once i click on Save but not when i click Cancel. Also for the Cancel button i am executing some code at the Server to take some actions.
3
2965
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 Delegates which can get this back, but couldN#t found out how it works.
2
5384
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 pressing the ENTER key on the keyboard. In this case, it would appear that whilst the code on the button is executing and therefore presumably the button actually has focus, the leave event in the user control is not fired. Should I be interfacing...
1
1808
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 GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { String cskey = "OnSubmitScript"; String scriptText = "return confirm('Are you sure you want to delete?')"; Type cstype = this.GetType();
0
1462
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 event, it's in On_Init method. Tested, this method is not invoked too. This happens only in when in SSL. Can anyone help me? Thanks. Artur
2
2857
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 need some custom javascript validation on form submit but why does is kill the submit button event handler? Submit buttons don't use __doPostBack so what is being effected?
0
9812
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9658
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10534
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7775
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6975
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5644
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5814
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4444
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.