Search code examples
htmlasp.netrequiredfieldvalidator

ASP.NET RequiredFieldValidator triggered from separate div


I have a table for user input, to add new employees. And for the attribute ID I want it to be required so I am using RequiredFieldValidator like this:

<td>ID:</td>
<td><asp:TextBox ID="AddIdTextBox" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator id="AddIdRequiredFieldValidator" runat="server"
        ControlToValidate="AddIdTextBox"
        ErrorMessage="ID is required"
        ForeColor="Red">
    </asp:RequiredFieldValidator>
</td>

On the same page but in another div, I have a ListView that shows all employees. When I click a button that is suppose to delete an employee, and If I did not enter an ID on the textbox above, the RequiredFieldValidator does not alow it.

Is it possible solve this?


Solution

  • Use ValidationGroup in the Validator and the corresponding Button that submits those fields.

    <asp:RequiredFieldValidator id="AddIdRequiredFieldValidator" runat="server"
        ControlToValidate="AddIdTextBox"
        ErrorMessage="ID is required"
        ForeColor="Red"
        ValidationGroup="Group1">
    </asp:RequiredFieldValidator>
    

    And also the Button.

    <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="Group1" />