按钮和验证器

发布于 2024-09-28 03:29:54 字数 179 浏览 7 评论 0原文

我有一个带有两个文本框的表。每个文本框都有一个必需的验证器来使用户在文本框中插入数据。

我还有一个登录表来确认用户权限。

当我提交登录按钮时,第一个表中的验证器出现并阻止用户登录。我将登录按钮属性(原因验证)更改为 false,但我在登录表中添加的验证器没有出现。

那么请问我该如何解决这个问题。

I have a Table with two textboxes. Every textbox has a required validator to make the user insert data in the textbox.

I also have a Login table to confirm user privilages.

When I submit the button of login, the validators from the first table appear and prevent the user from logging in. I changed the login button property (Causes validation ) to false, but the validators which I added in the login table didn't appear.

So please how can I solve this problem.

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

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

发布评论

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

评论(2

神仙妹妹 2024-10-05 03:29:54

我认为您可以通过使用ValidationGroups来解决您的问题。以下是带有较长说明的页面的摘录适合您:

此页面有两组验证者——“Group1”和“Group2”。然后页面上有两个按钮 - 单击“button1”时,将触发第一组验证器。单击“button2”时,将触发第二组验证器。如果验证失败,回发将默认被阻止客户端:

<html>
<body>
     <form runat=“server”>
          <asp:textbox id=“TextBox1” runat=“server”/>
          <asp:requiredfieldvalidator ValidationGroup=“Group1”
                                                       ErrorText=“Need to Fill in Value!”
                                                       ControlToValidate=“TextBox1”
                                                       runat=“server”/>
            <asp:textbox id=“TextBox2” runat=“server”/>
            <asp:requiredfieldvalidator ValidationGroup=“Group2”
                                                         ErrorText=“Need to Fill in Value!”
                                                         ControlToValidate=“TextBox2”
                                                         runat=“server”/>
            <asp:button text=“Group1” ValidationGroup=“Group1” runat=“server”/>
            <asp:button text=“Group2” ValidationGroup=“Group2” runat=“server”/>
     </form>
</body>
</html>

I think you can solve your problem by using ValidationGroups. Here is an excerpt from a page with a longer explanation for you:

This page has two groups – a “Group1” and a “Group2” of validators. There are then two buttons on the page – when button1 is clicked, the first group of validators will fire. When button2 is clicked, the second group of validators will fire. Postback will be blocked client-side by default if the validation fails:

<html>
<body>
     <form runat=“server”>
          <asp:textbox id=“TextBox1” runat=“server”/>
          <asp:requiredfieldvalidator ValidationGroup=“Group1”
                                                       ErrorText=“Need to Fill in Value!”
                                                       ControlToValidate=“TextBox1”
                                                       runat=“server”/>
            <asp:textbox id=“TextBox2” runat=“server”/>
            <asp:requiredfieldvalidator ValidationGroup=“Group2”
                                                         ErrorText=“Need to Fill in Value!”
                                                         ControlToValidate=“TextBox2”
                                                         runat=“server”/>
            <asp:button text=“Group1” ValidationGroup=“Group1” runat=“server”/>
            <asp:button text=“Group2” ValidationGroup=“Group2” runat=“server”/>
     </form>
</body>
</html>
孤独陪着我 2024-10-05 03:29:54

您需要在按钮和验证上使用 ValidationGroup 属性。这允许某些操作在单击按钮时仅在页面上强制执行验证器的子集。

<asp:TextBox ID="txtA" runat="server" />
<asp:RequiredFieldValidator ID="rfvA" runat="server" ErrorMessage="Message." ControlToValidate="txtA" ValidationGroup="A" />
<asp:Button ID="btnA" runat="server" Text="A" ValidationGroup="A" />

<asp:TextBox ID="txtB" runat="server" />
<asp:RequiredFieldValidator ID="rfvB" runat="server" ErrorMessage="Message." ControlToValidate="txtA" ValidationGroup="B" />
<asp:Button ID="btnB" runat="server" Text="B" ValidationGroup="B" />

现在当点击btnA时,它只会检查rfvA是否有效(检查txtA),当点击btnB时,它只会检查rfvB是否有效。是的,您可以在同一个验证组中拥有多个验证控件。

当您将 CausesValidation 属性设置为 false 时,您将禁用按钮的所有验证操作,而不仅仅是您不想要的验证操作。

What you need to use is a ValidationGroup attribute on both the buttons and the validations. This allows certain actions to only enforce a subset of validators on the page when the button is clicked.

<asp:TextBox ID="txtA" runat="server" />
<asp:RequiredFieldValidator ID="rfvA" runat="server" ErrorMessage="Message." ControlToValidate="txtA" ValidationGroup="A" />
<asp:Button ID="btnA" runat="server" Text="A" ValidationGroup="A" />

<asp:TextBox ID="txtB" runat="server" />
<asp:RequiredFieldValidator ID="rfvB" runat="server" ErrorMessage="Message." ControlToValidate="txtA" ValidationGroup="B" />
<asp:Button ID="btnB" runat="server" Text="B" ValidationGroup="B" />

Now when btnA is clicked, it will only check whether rfvA is valid (checking txtA) and when btnB is clicked, it will only check whether rfvB is valid. And yes you can have multiple validation controls in the same validation group.

When you set the CausesValidation property to false, you were disabling all validation actions for the button, not just the ones you didn't want on.

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