Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.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
为什么jquery.validate似乎不能与用户控件一起工作?_Jquery_Asp.net_Webforms - Fatal编程技术网

为什么jquery.validate似乎不能与用户控件一起工作?

为什么jquery.validate似乎不能与用户控件一起工作?,jquery,asp.net,webforms,Jquery,Asp.net,Webforms,我正在编写一个用户控件,它使用jquery.validate而不是验证控件来处理必填/电子邮件字段。我建立了一个快速测试页面,一切正常。但是,如果我将一些功能提取到一个web控件中并将其放到页面上,那么后面代码中的ifPage.IsValid{}检查似乎总是正确的,并且验证似乎不再有效。以下是我用于web控件的代码: 前端: <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" I

我正在编写一个用户控件,它使用jquery.validate而不是验证控件来处理必填/电子邮件字段。我建立了一个快速测试页面,一切正常。但是,如果我将一些功能提取到一个web控件中并将其放到页面上,那么后面代码中的ifPage.IsValid{}检查似乎总是正确的,并且验证似乎不再有效。以下是我用于web控件的代码:

前端:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" I          nherits="JqueryValidateTest.TestControl" %>
<div id="control-wrapper">
    <asp:TextBox ID="txtName" runat="server" ClientIDMode="Static"></asp:TextBox>
    <asp:TextBox ID="txtEmail" runat="server" ClientIDMode="Static"></asp:TextBox>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
        onclick="btnSubmit_Click" />
</div>
这是控件放置到的页面的html/js:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="JqueryValidateTest.Default" %>

<%@ Register Src="TestControl.ascx" TagName="TestControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        input.valid
        {
            border: 2px solid lime;
        }
        input.error
        {
            border: 2px solid red;
        }
    </style>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js">     </script>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script>
    <script type="text/javascript">
    $(document).ready(function () {
        jQuery.validator.messages.required = "";
        jQuery.validator.messages.email = "";
        $('#form1').validate({
            rules:
            {
                txtName: {
                    required: true
                },
                txtEmail: {
                    required: true,
                    email: true
                }
            },
            messages: {}
        });
    });
    </script>
</head>
<body>
<form id="form1" runat="server">
<div>
    <uc1:TestControl ID="TestControl1" runat="server" />   
</div>
</form>
</body>
</html>
我认为这可能与控件ID的更改有关,但我已将ClientIDMode更改为static,它们似乎保持不变

有人知道为什么会失败吗

谢谢你使用这个:

<%= txtName.UniqueID %> {
                required: true
            },
 <%= txtEmail.UniqueID %> : {
                required: true,
                email: true
            }
使用以下命令:

<%= txtName.UniqueID %> {
                required: true
            },
 <%= txtEmail.UniqueID %> : {
                required: true,
                email: true
            }

控件ID在NamingController的层次结构后动态命名,其中包括UserControl

第一种解决方案:您可以对字段、页面或用户控件使用clientdmode=static,但必须注意不要将两个控件命名为同一个控件

第二种解决方案:在用户控件中添加javascript验证规则,在以下未测试的代码中引用控件的ClientID:

$(function () {
    $('#form1').rules("add",
        {
            <%= txtName.ClientID %>: {
                required: true
            },
            <%= txtEmail.ClientID %>: {
                required: true,
                email: true
            }
        }
    );
});

控件ID在NamingController的层次结构后动态命名,其中包括UserControl

第一种解决方案:您可以对字段、页面或用户控件使用clientdmode=static,但必须注意不要将两个控件命名为同一个控件

第二种解决方案:在用户控件中添加javascript验证规则,在以下未测试的代码中引用控件的ClientID:

$(function () {
    $('#form1').rules("add",
        {
            <%= txtName.ClientID %>: {
                required: true
            },
            <%= txtEmail.ClientID %>: {
                required: true,
                email: true
            }
        }
    );
});

我试过了,但没用。。大概是因为该代码位于.aspx页面上,其中as控件仅在.ascx控件的范围内可用?如果我将该脚本放在用户控件中,它似乎可以工作。。我真傻!谢谢你的帮助!我试过了,但没用。。大概是因为该代码位于.aspx页面上,其中as控件仅在.ascx控件的范围内可用?如果我将该脚本放在用户控件中,它似乎可以工作。。我真傻!谢谢你的帮助!