Multiple RequiredFieldValidators on page but they need to apply to different button clicks

前端 未结 2 848
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-13 05:16

I\'m working on a asp.net page and I have the following scenario:

I have 2 fields that have requiredfieldvalidators which need to \"fire\" their validation when butt

相关标签:
2条回答
  • 2021-01-13 06:13

    You can use Validation Group.

    Sample code here:

    <body>
        <form id="form1" runat="server">
        <div>
            <h1>Group1</h1>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" ValidationGroup="group1"></asp:RequiredFieldValidator>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox2" ValidationGroup="group1"></asp:RequiredFieldValidator>
    
            <br /><br />
            <h1>Group2</h1>
            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox3" ValidationGroup="group2"></asp:RequiredFieldValidator>
            <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox4" ValidationGroup="group2"></asp:RequiredFieldValidator>
    
            <br /><br />
            <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="group1" />
            <asp:Button ID="Button2" runat="server" Text="Button" ValidationGroup="group2" />
        </div>
        </form>
    </body>
    
    0 讨论(0)
  • 2021-01-13 06:17

    Use validation groups. See the next article http://www.dotnet-guide.com/validationgroups.html.

    0 讨论(0)
提交回复
热议问题