如何让jquery数据验证程序与Asp.net gridview一起工作

如何让jquery数据验证程序与Asp.net gridview一起工作,jquery,asp.net,gridview,validation,Jquery,Asp.net,Gridview,Validation,我一直在尝试实现jquery验证器,这是这里建议的答案: 但在本例和我发现的所有其他示例中,都要求要验证的元素以某种形式存在。这可能是一个新手问题,但我在asp:gridview中有一个asp:textbox(而不是输入),我无法让验证工作。有没有一种方法可以将其中的一部分嵌套到表单中,或者这些asp函数中的一个函数是否会自动在html中生成表单 如果有帮助,下面是我正在使用的jquery代码和我的gridview: <script type = "text/javascript">

我一直在尝试实现jquery验证器,这是这里建议的答案:

但在本例和我发现的所有其他示例中,都要求要验证的元素以某种形式存在。这可能是一个新手问题,但我在asp:gridview中有一个asp:textbox(而不是输入),我无法让验证工作。有没有一种方法可以将其中的一部分嵌套到表单中,或者这些asp函数中的一个函数是否会自动在html中生成表单

如果有帮助,下面是我正在使用的jquery代码和我的gridview:

 <script type = "text/javascript">
 $(function() {     
 // You can specify some validation options here but not rules and messages
      $('form').validate();
      // Add a custom class to your name mangled input and add rules like this     
      $('textbox[id$=NPI]').rules('add', {
          required: true,
          messages: {
              required: 'Some custom message for the username required field'
          }
       });
       }); 
    </script>

 <div style="overflow:auto; height:300px;">
<asp:GridView ID="SetRules" runat="server" AutoGenerateColumns="False" DataSourceID="AttributesRules" OnDataBound="Anchor_Changed"
    class="styleGrid archGrid validation" AlternatingRowStyle-CssClass = "styleGridAlt" DataKeyNames="banner, pricinggroupkey, attribute, tieranchor, tierother">
    <Columns>                                               
    <asp:TemplateField HeaderText="New Price Index" SortExpression="NewPriceIndex">               
            <ItemTemplate>
                <asp:TextBox ID="NPI" class="NumVal" runat="server" 
                    Text='<%# Bind("NewPriceIndex", "{0:N2}") %>'></asp:TextBox>
            </ItemTemplate>  
        </asp:TemplateField>

    <asp:TemplateField HeaderText="New Index Range" SortExpression="NewIndexRange">               
            <ItemTemplate>
                <asp:TextBox ID="NIR" class="NumVal" runat="server" 
                    Text='<%# Bind("NewIndexRange", "{0:N2}") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>           
                </Columns>
</asp:GridView>
</div>

$(函数(){
//您可以在此处指定一些验证选项,但不能指定规则和消息
$('form').validate();
//将自定义类添加到名称已损坏的输入中,并添加如下规则
$('textbox[id$=NPI]')。规则('add'{
要求:正确,
信息:{
必需:“用户名必填字段的某些自定义消息”
}
});
}); 

默认情况下,ASP.NET代码将在一组表单标记(ASP.NET要求)内呈现,而文本框元素将呈现为“输入”元素。查看呈现页面的HTML以查看输出在浏览器中的外观。选择器应使用以下语法引用ASP.NET自动生成ID

$('#<%= NIR.ClientID %>')
$(“#”)

不要忘记在ASP.NET代码之前添加“#”符号,因为您使用的是jQuery ID选择器。

谢谢,这就是我所需要的