分别验证Div标签

时间:2010-04-09 12:42:56

标签: asp.net javascript vb.net

我有3个div标签。每个都没有。的文本框和按钮。每个文本框都有验证。对于所有按钮causevalidation属性为true。如果我单击“div1”标签中的“button1”将导致其他div标签中的其他文本框验证。我不想这样做。怎么避免这个?

2 个答案:

答案 0 :(得分:3)

将每个div中元素的ValidationGroup属性设置为单独的验证组。

示例:

<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="Div1"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="Div1"></asp:Button>
<asp:RequiredFieldValidator ControlToValidate="Textbox1" ValidationGroup="Div1" ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>

<asp:TextBox ID="TextBox2" runat="server" ValidationGroup="Div2"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Button" ValidationGroup="Div2"></asp:Button>
<asp:RequiredFieldValidator ControlToValidate="Textbox2" ValidationGroup="Div2" ID="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>

<asp:TextBox ID="TextBox3" runat="server" ValidationGroup="Div3"></asp:TextBox>
<asp:Button ID="Button3" runat="server" Text="Button" ValidationGroup="Div3"></asp:Button>
<asp:RequiredFieldValidator ControlToValidate="Textbox3" ValidationGroup="Div3" ID="RequiredFieldValidator3" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>

答案 1 :(得分:2)

您应该使用Validation Groups