Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ValidateUserName未定义_C#_Asp.net_Web Services_Jquery - Fatal编程技术网

C# ValidateUserName未定义

C# ValidateUserName未定义,c#,asp.net,web-services,jquery,C#,Asp.net,Web Services,Jquery,我有一个jquery ajax调用asp.net web服务的问题。我想验证asp.net网页中的文本框。 验证器是: <asp:CustomValidator ID="CustomValidatorUser" runat="server" ControlToValidate="TextUserName" ErrorMessage="Minimum of 6 (six) alphanumeric characters."

我有一个jquery ajax调用asp.net web服务的问题。我想验证asp.net网页中的文本框。 验证器是:

<asp:CustomValidator ID="CustomValidatorUser" runat="server" ControlToValidate="TextUserName"
                                ErrorMessage="Minimum of 6 (six) alphanumeric characters." 
                 ClientValidationFunction="ValidateUserName" Display="Dynamic"
                                ValidateEmptyText="True" ></asp:CustomValidator>
data: "{'strUsername': " + $("#<%=TextUserName.ClientID%>").val() + "}",
但我有一个错误:

ValidateUserName is undefined.
data: "{'strUsername': " + $("#<%=TextUserName.ClientID%>").val() + "}",
请帮我纠正这个错误

data: "{'strUsername': " + $("#<%=TextUserName.ClientID%>").val() + "}",

多谢各位

您需要将ajax函数封装在函数调用
ValidateUserName
中,以便它匹配
ClientValidationFunction

function ValidateUserName(){
  $.ajax({
    type: "POST",
    url: "UserNameWebService.asmx/ValidateUserName",
    data: "{'strUsername': " + $("#TextUserName").val() + "}",
    contentType: "application/json; charset=utf-8",
    dataType: "json"
  });
}
data: "{'strUsername': " + $("#<%=TextUserName.ClientID%>").val() + "}",
即使如此,这也不会返回值。我认为您需要使用
$.ajax
函数的
success
选项来设置一个布尔值以返回该函数

data: "{'strUsername': " + $("#<%=TextUserName.ClientID%>").val() + "}",
所以你可以这样做:

function ValidateUserName(){
   var isValid;
   $.ajax({
    type: "POST",
    url: "UserNameWebService.asmx/ValidateUserName",
    data: "{'strUsername': '" + $("#TextUserName").val() + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json"
    , async: false
    , success: function(data){
       isValid = data;
    }
  });
  return isValid;
}
data: "{'strUsername': " + $("#<%=TextUserName.ClientID%>").val() + "}",

请注意,我还将
async
设置为false。

您的代码实际上有几个问题

data: "{'strUsername': " + $("#<%=TextUserName.ClientID%>").val() + "}",
1)
$(“#TextUserName”)
在此上下文中不起作用,因为asp.net将使用不同的ID呈现服务器端
TextBox
控件。您需要执行以下操作:

data: "{'strUsername': " + $("#<%=TextUserName.ClientID%>").val() + "}",
3) 您需要将jQueryAjax调用放在一个函数中,在您的例子中,这个函数被称为
ValidateUserName
。它采用两个参数
source
args
。此函数的职责是将
args.IsValid
的值设置为
true
false
。因此,您需要提供一个在ajax调用成功时调用的函数来执行此逻辑。像这样:

data: "{'strUsername': " + $("#<%=TextUserName.ClientID%>").val() + "}",
function ValidateUserName(source, args) {
    $.ajax({
        type: "POST",
        url: "UserNameWebService.asmx/ValidateUserName",
        data: "{'strUsername': '" + args.Value + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function (result) {
            args.IsValid = result.d;
        }
    });
}
4) 正如您在上面的代码中所看到的,您实际上不需要使用jquery从文本框中获取值,因为您可以像这样访问它
args.value

data: "{'strUsername': " + $("#<%=TextUserName.ClientID%>").val() + "}",

5) 您需要添加
async:false
,否则在设置
IsValid
时,设置消息可见性的代码将已经执行,因此不会发生任何事情。

能否显示整个验证函数?对不起,我指的是javascript函数。仅此而已。请看。为什么您的AJAX代码没有包装在
函数ValidateUserName(){…}
中?正如本文所述,在页面加载时,您似乎正在立即运行AJAX代码,而AJAX web参考脚本代码可能在那时还没有加载。。。
data: "{'strUsername': " + $("#<%=TextUserName.ClientID%>").val() + "}",