Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 867
  • Last Modified:

Conditional RequiredFieldValidator


I'm wondering if it is possible to cause the RequiredFieldValidator to validate ONLY when a specific button is pressed on a form, but none others.  Example:

<form id="form" runat="server">
   <asp:TextBox id="textbox1" runat="server" />
   <asp:RequiredFieldValidator id="vld1" runat="server" ErrorMessage="Required" ControlToValidate="textbox1" /><br />
   <asp:TextBox id="textbox2" runat="server" />
   <asp:RequiredFieldValidator id="vld2" runat="server" ErrorMessage="Required" ControlToValidate="textbox2" />

   <asp:Button id="btnAddRow" Text="Add New Field" onclick="btnAddNewField_Click" />
   <asp:Button id="btnSubmit" Text="Submit" onclick="btnSubmit_Click" />

When the user clicks "Submit", I want the field validators to work (and display the corresponding error message, if necessary).  If the user clicks the "Add New Field" button, I want the form to add a new field, but not display the error messages during postback.  How do I prevent the validator errors from appearing?  Is there a way I can "clear" the IsValid status of the page?  I'd like to avoid using a Response.Redirect if possible, only for cleanliness.
1 Solution

Set the CausesValidation property True for button btnSubmit but False for btnAddRow.

Try this...
hendridmAuthor Commented:
Thanks much!  Worked perfectly.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now