Asp.net 将jquery与嵌套母版页一起使用

Asp.net 将jquery与嵌套母版页一起使用,asp.net,jquery,master-pages,Asp.net,Jquery,Master Pages,有人能告诉我如何在asp.net嵌套母版页中使用jquery吗。我在主页上添加了jquerylibaray的链接和验证框架。然后,我创建了另一个带有一些样式的母版页,并基于该母版页创建了一个aspx页面 如何将验证框架附加到页面中的文本框 我试过了 $("#aspnetForm").validate({ rules: { <%=txtPostCode.UniqueID %>: {

有人能告诉我如何在asp.net嵌套母版页中使用jquery吗。我在主页上添加了jquerylibaray的链接和验证框架。然后,我创建了另一个带有一些样式的母版页,并基于该母版页创建了一个aspx页面

如何将验证框架附加到页面中的文本框

我试过了

         $("#aspnetForm").validate({
            rules: {
                <%=txtPostCode.UniqueID %>: {
                    minlength: 2,
                    required: true
                },
                 <%=txtContactEmail.UniqueID %>: {                        
                    required: true,
                    email:true
                }
            }, messages: {
                <%=txtPostCode.UniqueID %>:{ 
                    required: "* Required Field *", 
                    minlength: "* Please enter atleast 2 characters *" 
                }
           }
        });
$(“#aspnetForm”)。验证({
规则:{
: {
最小长度:2,
必填项:true
},
: {                        
要求:正确,
电子邮件:真的
}
},讯息:{
:{ 
必填项:“*必填字段*”,
minlength:*请输入至少2个字符*
}
}
});

然而什么也没发生。有人能给我指出正确的方向吗?

因为ASP.NET会破坏ID,所以最好的办法可能是为服务器控件设置CSS类,并以这种方式访问它们:

<asp:textbox id="Text1" cssclass="myText" runat="server" />

$('.myText').whatever();

$('.myText').whatever();

它不是最有效的JS,因为您选择的是按类而不是ID,但它可以避免您在WebForms中处理ASP.NET损坏的ID时遇到的麻烦。

由于ASP.NET会损坏ID,您最好的选择可能是为您的服务器控件设置CSS类并以这种方式访问它们:

<asp:textbox id="Text1" cssclass="myText" runat="server" />

$('.myText').whatever();

$('.myText').whatever();

它不是最有效的JS,因为您选择的是按类而不是ID,但它可以避免您在WebForms中处理ASP.NET损坏的ID时遇到的麻烦。

您可以编写将ID传递给jQuery函数的包装器方法,并使用子页面上的
RegisterStartupScript
或内联JS调用这些方法:

Javascript:

function makejQueryOnMangledNamesLessPainful(firstSelector, secondSelector)
{
    $(firstSelector).whatever();
}
.aspx:

<script>
   makejQueryOnMangledNamesLessPainful('#<%=Thing.ClientID%>',
                                       '<%=OtherThing.GetClientSelector()%>);
</script>

使jQueryOnMangledNamesLessPanful(“#”,
');

其中
GetClientSelector
返回一个作为选择器的字符串,该字符串可能只是一个ID,也可能是某个服务器派生的类集。或者其他任何方法。

您可以编写将ID传递给jQuery函数的包装器方法,并使用子页面上的
RegisterStartupScript
或内联JS调用这些方法:

Javascript:

function makejQueryOnMangledNamesLessPainful(firstSelector, secondSelector)
{
    $(firstSelector).whatever();
}
.aspx:

<script>
   makejQueryOnMangledNamesLessPainful('#<%=Thing.ClientID%>',
                                       '<%=OtherThing.GetClientSelector()%>);
</script>

使jQueryOnMangledNamesLessPanful(“#”,
');
其中
GetClientSelector
返回一个作为选择器的字符串,该字符串可能只是一个ID,也可能是某个服务器派生的类集。或者别的什么,真的