Conditional RequiredFieldValidator

Posted on 2004-11-24
Last Modified: 2008-01-09

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.
Question by:hendridm
    LVL 1

    Accepted Solution


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

    Try this...
    LVL 8

    Author Comment

    Thanks much!  Worked perfectly.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video discusses moving either the default database or any database to a new volume.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now