views:

177

answers:

1

I am trying to attach an event handler to form.submit on asp.net rendered pages with no success. I want to intercept every postback, and doc. says that I should be able. Am I doing something wrong?

$(document).ready(function() {
    $("form").submit(function() {
            alert('Form submit');
            debugger;
    });
});
+2  A: 

asp.net webforms are generally enveloped in only one big form (hence the term web forms).

if I'm not mistaken, all links and submit buttons call __doPostBack manually, so it bypasses the calling form submit and delegates that responsibility to the __doPostBack function.

you would probably need to overwrite this function.

<script type="text/javascript"> 
//<![CDATA[
var theForm = document.forms['aspnetForm'];
if (!theForm) {
    theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script> 

though I'm trying to understand your "debugger;" line in your function. perhaps it should be this?

$(document).ready(function() {
    $("form").submit(function() {
            alert('Form submit');
    });
}); 
michael herndon
debugger allows me to step into javascript code and debug from VS; You are right about _doPostBack, I thought that jQueary would attach handler to the form.submit().
epitka
event bubbling could be an issue.another function attached to onsubmit could be calling event.stopPropagation and event.preventDefault()__doPostBack is checking the onsubmit and if any other submit handlers get attached to the event before yours does and they return false, or prevent from bubbling up, your submit handler will never get called.
michael herndon