Disable RequiredFieldValidators inside a DetailsView

Posted on 2006-05-13
Last Modified: 2008-01-09
I have a form that contains a GridView that lists and modifies, and a DetailsView that inserts new rows into a database table. I assigned RequiredFieldValidators to all the fields of the DetailsView. From that time on editing a row in the GridView doesn't work because the validators prevent me from applying the updates.

How can they be disabled during the updating? They can't be accessed by ID, because they are inside an <InsertItemTemplate>. Is there an elegant solution?
Question by:pallosp
    LVL 7

    Accepted Solution

    1) If u want to diable all the validation controls in the page use either of the following 2.
        a)  In the itemdatabound event set the onlick attribute to the update control like btnUpdate.Attributes.Add("onclick",="Page_ValidationActive=false;")
        b) else set btnUpdate.CausesValidation = false.

    2) Suppose if u want to disable particular controls then try the following
    <script language=javascript>
    function Disablevalidators()
                      var len = Page_Validators.length;
                      var id="";
                            id = Page_Validators[icount].id;
                            if(id.indexOf("validator1") == -1 && id.indexOf("validator2") == -1)
                                  Page_Validators[icount].enabled = false;      

    i assumed u want to have only 2 validators(validator1,validator2).. You can replace the condition with the no of controls u wanted.

    Hope this helps you,
    LVL 9

    Author Comment

    The edit template of the gridview, and the insert template of the detailsview both contain validators. The default mode of the detailsview is inserting.

    The behavior that I want is the following:
    - when sy clicks on the edit link of a row, the validators of that row are enabled, and the detailsview with the corresponding validators are disabled
    - after updating the row or clicking on the cancel link, the validators are disabled in that row and enabled in the detailsview again

    1. How can I access btnUpdate? It's an embedded control inside the gridview.
    2. Who will call the Disablevalidators() function? I'd like to avoid using JavaScript and if possible, doing everything from server side.
    LVL 7

    Expert Comment

    hi pall,
    1. Hope you can get the btnUpdate control in the rowdatabound. like e.Row.FindControl
    2. Once u get the control u can add attributes. But the still the validatations are going to be in client side.

    Tell me if it helps you or not..

    LVL 9

    Author Comment

    I found the solution. The key concept is ValidationGroup.

    A part of code inside asp:GridView/Columns

    <asp:TemplateField HeaderText="Search term">
            <asp:TextBox ID="txtSearchTerm" runat="server" Text='<%# Bind("searchTerm") %>'></asp:TextBox>
            <asp:RequiredFieldValidator ID="reqSearchTerm" runat="server" ErrorMessage="*" ControlToValidate="txtSearchTerm" ValidationGroup="vgrpUpdate"/>
        <ItemTemplate />
        <ItemStyle Wrap="False" />


    <asp:TemplateField HeaderText="Edit">
            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
            <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"/>

    The rows in asp:DetailsView are similar, but they have different validation group.

    Thanks for your help anyway

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    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…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 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

    13 Experts available now in Live!

    Get 1:1 Help Now