Search code examples
asp.netrequiredfieldvalidator

Required field validator firing for other control


I have a page for modification of customer information, I allow two textboxes on the page for each piece of customer information, so if customer has 5 piece of information, then my page will have 10 textboxes, one for showing the old value and one for entering the new value. All the textboxes that have original information are visible and then based on which column user selects to modify that columns textbox is made visible.

There are required field validators for all the textboxes which will be used to enter the new value. We allow single field modification. The problem that I am facing is, suppose the user selected to modify the designation field of the user and the designation field does not have any value, and then some other field let say the organization name does not have a value, only the selected fields corresponding textbox will be visible, when I try to submit the information it gives me error that Organization name is required, even though the new textbox for organization name is not visible, any ideas as to why this is occurring?

EDIT:

<td align="right"><span>Designation:</span></td>
<td>
    <asp:TextBox runat="server" ID="ModifyCustomerByCategorytxtDesignation"
        class="ModifyCustomerByCategoryTextbox"></asp:TextBox>
</td>
<td>
    <asp:TextBox runat="server" ID="ModifyCustomerByCategorytxtNewDesignation"
        class="ModifyCustomerByCategoryTextbox"></asp:TextBox>
    <asp:RequiredFieldValidator ValidationGroup="Group6"
        ControlToValidate="ModifyCustomerByCategorytxtNewDesignation" runat="server"
        EnableClientScript="true" Display="Dynamic" 
        ErrorMessage="The Designation field is required." Text="*">
    </asp:RequiredFieldValidator>
</td> 

Solution

  • If you only allow a single field modification, then add a separate button for each field, and make it share a unique ValidatinGroup with that's field validator (your problem is because they all share a single ValidatinGroup, so they all get validated, visible or not).