如何在一个页面中使用多个验证

时间:2014-03-02 11:47:52

标签: c# asp.net validation requiredfieldvalidator validationsummary

我在一组中有2个单选按钮(radiobtn1和radiobtn2)和4个文本框,名称是a,b,c,d。页面末尾有标准按钮。我将asp:requiredFieldValidators添加到所有文本框中。因此,当我点击我的按钮时,asp:validationsummary适用于所有文本框。

如果单击radiobtn1,我想这样做,validationsummary应该适用于文本框a和b,如果单击radiobtn2,validationsummary应该适用于textbox c和d。

我该怎么做?

THX。

1 个答案:

答案 0 :(得分:1)

这符合您的要求..

  <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="*" Font-Size="Medium" ForeColor="Red" ValidationGroup="group 1">*</asp:RequiredFieldValidator>
        <br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2" ErrorMessage="*" Font-Size="Medium" ForeColor="Red" ValidationGroup="group 1">*</asp:RequiredFieldValidator>
        <br />
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TextBox3" ErrorMessage="*" Font-Size="Medium" ForeColor="Red" ValidationGroup="group 2">*</asp:RequiredFieldValidator>
        <br />
        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="TextBox4" ErrorMessage="*" Font-Size="Medium" ForeColor="Red" ValidationGroup="group 2">*</asp:RequiredFieldValidator>
        <br />
        <br />
        <br />
        <br />
        <br />
        <asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True"  Text="valid first 2" CausesValidation="True" GroupName="g" ValidationGroup="group 1"/>
&nbsp;   &nbsp;  <asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="True" Text="valid last 2" CausesValidation="True" GroupName="g" ValidationGroup="group 2" />
        <br />
        <br />
    </div>
    </form>