button and validators

207 views Asked by At

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.

2

There are 2 answers

0
DOK On BEST ANSWER

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>
0
ben f. On

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.