ASP.NET自定义验证+ +的WebMethod jQuery的 [英] ASP.NET Custom Validator + WebMethod + jQuery

查看:214
本文介绍了ASP.NET自定义验证+ +的WebMethod jQuery的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现使用$就查询在同一页上一个WebMethod,并返回一个布尔值一个.NET自定义验证,表明结果是否是真还是假。

I'm trying to implement a .NET Custom Validator that uses $.ajax to query a WebMethod on the same page and return a boolean value to indicate whether the result is true or false.

我使用的是的WebMethod很简单

[WebMethod()]
public static bool IsPromoValid(string code)
{
    string promoCode = "ABCDEFG";
    bool result = code.ToLower() == promoCode.ToLower();
    return result;
}

总的CustomValidator看起来像这样

<asp:CustomValidator ID="cvPromoCode" Display="None" ControlToValidate="txtPromoCode" runat="server" ClientValidationFunction="validatePromo"
    ErrorMessage="The promo code you entered is incorrect" OnServerValidate="ValidatePromoCode" />

和简单的$。阿贾克斯() ClientValidation 功能

And the simple $.ajax() ClientValidation function

function validatePromo(src, args) {
    $.ajax({
        type: "POST",
        url: "Register.aspx/IsPromoValid",
        data: "{'code': '" + args.Value + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            args.IsValid = msg.d;
        }
    });
}

问题是,页面立即进行验证,并不实际等待Ajax调用来完成。如果在页面上的任何其他错误,就说明他们的验证摘要,但从未表示从自定义验证器的错误信息。

The problem is that the page validates instantly and doesn't actually wait for the ajax call to finish. If there are any other errors on the page, it shows the Validation Summary with them, but never shows the error message from the Custom Validator.

我可以看到AJAX调用中的萤火虫出现了,它returs正确的反应(在本例中的真正的或的

I can see the AJAX call being made in Firebug, and it returs the right response (in this case true or false)

推荐答案

容易的方法是通过改变你的validate为:

The easy way is by changing your validate to:

 function validatePromo(src, args) {
    var isValid;
    $.ajax({
        type: "POST",
        url: "Register.aspx/IsPromoValid",
        data: "{'code': '" + args + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function (msg) {
             isValid = msg.d;
        }        
    });
    args.IsValid = isValid;
}

请特别注意的异步:假。你的第一次尝试没有工作的原因是,阿贾克斯成功回调没有得到调用后验证脚本已经检查args.IsValid到。随着异步:假的,$就调用将无法完成,直到成功回调完成后

Take special note of the async:false. The reason your first try didn't work is that the ajax success callback was not getting called until after the validation scripts had already checked args.IsValid. With async:false, the $.ajax call won't complete until after the success callback is done.

这样做的最大的问题是,它现在的块无论JS线程运行的验证。在ASP.Net验证的情况下,我不相信这是一个问题,但我会长期运行的呼叫测试它只是为了确保你没有连接速度慢搞砸了你的页面的人。

The big issue with this is that it now "blocks" whatever js thread is running the validation. In the case of ASP.Net validators, I don't believe this is an issue but I would test it with a long-running call just to make sure you aren't screwing up your page for anyone on a slow connection.

这篇关于ASP.NET自定义验证+ +的WebMethod jQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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