开发者

Suppressing .net's updatepanel submit event handler

开发者 https://www.devze.com 2023-02-11 11:02 出处:网络
I\'m performing some custom js validation on my forms, triggered by submitting the form $(\"form\").submit(function (e) {

I'm performing some custom js validation on my forms, triggered by submitting the form

    $("form").submit(function (e) {
        var validates = true;
        // validation code goes in here
        if (!validates) {
            e.preventDefault();
            e.stopImmediatePropagation();
            return false;
        }
    开发者_StackOverflow})

None of the lines I've included to stop the submit event prevent the .NET control posting back. Is there some way (using js or setting a property in the updatepanel control) of stopping post back?


I found the reliable way to do this is to use the Sys.WebForms.PageRequestManager class:

Sys.WebForms.PageRequestManager.getInstance()
    .add_initializeRequest(function (sender, args) {
        if (!function_to_check_form_is_valid(sender, args))
            args.set_cancel(true);
        }
   });


The easiest way to do it is to capture client click on the thing that is submitting the form.


Is there any reason you are doing client side validation instead of server side? If it were me, I would just write a custom asp.net validator, then check against that validator before sending the update. The update panel will still postback, but as long as you check against the validator, nothing will have been created.

0

精彩评论

暂无评论...
验证码 换一张
取 消