页面上有多个RequiredFieldValidators,但它们需要应用于不同的按钮点击

发布于 2024-09-27 10:34:09 字数 277 浏览 3 评论 0原文

我正在开发一个 asp.net 页面,并且有以下场景:

我有 2 个具有 requiredfieldvalidators 的字段,这些字段需要在单击“button1”时“触发”验证,但在单击“button2”时则不需要“触发”验证,另一个字段由另一个字段验证requiredfieldvalidator 与相反的场景。 (此字段的 requiredfieldvalidator 需要在单击“button2”时“触发”,但在单击“button1”时则不需要。)任何有关最简单解决方案的建议将不胜感激。

提前致谢

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 button1 is clicked but NOT when button2 is clicked and another field which is validated by another requiredfieldvalidator with the opposite scenario. (requiredfieldvalidator for this field needs to "fire" when button2 is clicked but NOT when button1 is clicked.) Any suggestions for the simplest solution would be appreciated.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微暖i 2024-10-04 10:34:09

您可以使用验证组

示例代码在这里:

<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>

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>
早茶月光 2024-10-04 10:34:09

使用验证组。请参阅下一篇文章 http://www.dotnet-guide.com/validationgroups.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文