防止__doPostBack触发 [英] Prevent __doPostBack from firing off

查看:111
本文介绍了防止__doPostBack触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格控件,允许用户向网格中添加一个值.

I have a grid control that allows users to add a value into the grid.

它会产生以下链接,用于插入全新商品.

It produces the following link for inserting a brand new item.

<a onclick="if(!$find('radGridHolidays_ctl00').insertItem()) return false;" id="radGridHolidays_ctl00_ctl02_ctl02_PerformInsertButton" href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("radGridHolidays$ctl00$ctl02$ctl02$PerformInsertButton", "", true, "", "", false, true))'>Insert</a>

我有一些自定义jQuery代码,可在提交前验证文本框.我试图阻止链接提交__doPostBack,但是我的代码无法正常工作.

I have some custom jQuery code that validates textboxes before the submit. I am trying to prevent the link from submitting the __doPostBack, but my code isn't working.

    // if need be, move this to its own .js file
    String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

    // Document loaded, let's register some validation
    $(function() {

        $('#holidayValidationDisplay').hide();

        $('a#radGridHolidays_ctl00_ctl02_ctl02_PerformInsertButton').click(function(e) {

            e.stopImmediatePropagation();
            e.preventDefault();
            var hasErrors = false;

            // alert('You clicked me!');
            var holidayName = $('#radGridHolidays_ctl00_ctl02_ctl02_validatorHolidayName').val().trim();
            var holidayDate = $('#radGridHolidays_ctl00_ctl02_ctl02_RDIPHolidayDate_dateInput_text').val().trim();

            if (holidayName.length == 0) {
                hasErrors = true;
                $('#holidayList').append('<li>You must provide a holiday name</li>');
            }

            if (holidayDate.length == 0) {
                hasErrors = true;
                $('#holidayList').append('<li>You must provide a holiday date</li>');
            }

            if (hasErrors) {
                $('#holidayValidationDisplay').show();
                return false;
            }
        });
    });

但是当我不希望发生错误时回发时,该链接仍处于断开状态.我看到了一些阻止它在UpdatePanel中工作的信息,但是我不确定那里的解决方案是否适用于我的情况.

but the link is still firing off when I don't want the postback to occur if there are errors. I saw something about preventing it to work in an UpdatePanel, but I'm not sure that the solution there applies to my scenario.

如果有人有任何想法,我都是耳朵".

If anyone has any ideas, I'm all "ears".

推荐答案

我相信您遇到的问题是该按钮不知道表单是否有效.这就是我要做的:

I believe the issue you're having is that the button has no knowledge if the form is valid or not. This is what I do:

编写一个自定义函数,该函数根据其他一些值返回true或false:

Write a custom function that returns true or false based on some other value:

function isTermsChecked(oSrc, args)
{
    args.IsValid = $("#chkTerms").is(':checked');
}

因此,在此示例中,如果选中了我的条款复选框,则args.IsValid为true.只需转到链接属性,然后应该有一个名为OnClientClick的属性.您将放置isTermsChecked.

So in this example, if my terms checkbox is checked, then args.IsValid is true. Just go to the link properties and there should be a property called OnClientClick. You would put isTermsChecked.

在您的特定情况下,您可能需要使用一个变量.如果jQuery无效,则将该变量设置为false,然后自定义验证函数将引用该变量.本质上,如果为true,则该按钮将继续显示,为false,否则不会.

In your specific case, you would probably want to use a variable. If jQuery comes up invalid, set that variable to false, and the custom validation function will reference that variable. Essentially if it's true, the button will proceed, false, and it won't.

编辑:如果您要对每一行进行验证,那么我看到的问题是,如果一行未通过验证,那么我上面的解决方案将禁用所有链接按钮,直到网格上的所有验证都完成为止通过了.您可能想忘记自定义功能,然后执行以下操作:

If you're doing validation on each row, the issue that I see is if one row fails validation then my solution above will make all link buttons disabled until all validation on the grid has passed. You may want to forget the custom function and do this:

如果jQuery验证失败,则在该行中找到链接按钮,并添加一个值为return falseOnClick属性,然后如果验证通过,则删除该属性.

If the jQuery validation fails, then find the link button in that row and add an OnClick attribute with the value return false then if validation passes, remove that attribute.

这篇关于防止__doPostBack触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆