How can the RequiredFieldValidator be bypassed upon clicking "Update" of gridview?

Posted on 2008-11-04
Last Modified: 2010-07-27
Hi Experts,

I have a form with InsertItemTemplate and a Gridview on the same page. I have a "RequiredFieldValidator" tied to a textbox on the form. Upon clicking "Insert" on the form the RequiredFieldValidator" is fired, but also when the Edit is clicked on the Gridview and Update is clicked, the RequiredFieldValidator fires as well. How can the RequiredFieldValidator tied to the form be disabled if the Edit of the Gridview is enabled?

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">


    <asp:FormView ID="FormView1" runat="server" DataKeyNames="CostCategoryID" DataSourceID="SqlDataSource1"

        DefaultMode="Insert" Width="632px">



            <asp:Label ID="CostCategoryIDLabel1" runat="server" Text='<%# Eval("CostCategoryID") %>'>

            </asp:Label><br />


            <asp:TextBox ID="CostCategoryTextBox" runat="server" Text='<%# Bind("CostCategory") %>'>

            </asp:TextBox><br />


            <asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>'>

            </asp:TextBox><br />

            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"



            <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"





            <strong>Cost Category:</strong>

            <asp:TextBox ID="CostCategoryTextBox" runat="server" Text='<%# Bind("CostCategory") %>'></asp:TextBox>

            <strong>Description: </strong>

            <asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>'

                Width="194px"></asp:TextBox><br />

            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="CostCategoryTextBox" SetFocusOnError="True" Display="Dynamic" ToolTip="Enter a Cost Category" ErrorMessage="The Cost Category field is required "></asp:RequiredFieldValidator><br />

            <br />

            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"


            <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"






            <asp:Label ID="CostCategoryIDLabel" runat="server" Text='<%# Eval("CostCategoryID") %>'></asp:Label><br />


            <asp:Label ID="CostCategoryLabel" runat="server" Text='<%# Bind("CostCategory") %>'></asp:Label><br />


            <asp:Label ID="DescriptionLabel" runat="server" Text='<%# Bind("Description") %>'></asp:Label><br />

            <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"


            <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"


            <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"




    <br />

    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"

        CellPadding="4" DataKeyNames="CostCategoryID" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display."

        ForeColor="#333333" GridLines="None" Width="628px">

        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />


            <asp:BoundField DataField="CostCategory" HeaderText="CostCategory" SortExpression="CostCategory" />

            <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />

            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />


        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />

        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />

        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

        <EditRowStyle BackColor="#999999" />

        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />


    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Logistics_Budget_ManagerConnectionString1 %>"

        DeleteCommand="DELETE FROM [Cost Category] WHERE [CostCategoryID] = @CostCategoryID"

        InsertCommand="INSERT INTO [Cost Category] ([CostCategory], [Description]) VALUES (@CostCategory, @Description)"

        ProviderName="<%$ ConnectionStrings:Logistics_Budget_ManagerConnectionString1.ProviderName %>"

        SelectCommand="SELECT [CostCategoryID], [CostCategory], [Description] FROM [Cost Category]"

        UpdateCommand="UPDATE [Cost Category] SET [CostCategory] = @CostCategory, [Description] = @Description WHERE [CostCategoryID] = @CostCategoryID">


            <asp:Parameter Name="CostCategoryID" Type="Int32" />



            <asp:Parameter Name="CostCategory" Type="String" />

            <asp:Parameter Name="Description" Type="String" />



            <asp:Parameter Name="CostCategory" Type="String" />

            <asp:Parameter Name="Description" Type="String" />

            <asp:Parameter Name="CostCategoryID" Type="Int32" />




Open in new window

Question by:indyng
    LVL 15

    Expert Comment

    You need to add validationgroup properties to the controls:
    In the FormView add ValidationGroup="FormGroup" to both the RequiredFieldValidator and the LinkButton that does the insert.

    To the CommandField button in the gridview add ValidationGroup="GridGroup".

    The names can be anything as long as the controls you want validated as a group all share the same name.
    LVL 1

    Author Comment


    How can a text box in the gridview be validated?
    LVL 15

    Accepted Solution

    If you want validation in the gridview then you need to use a templatefield and manually add the textbox and requiredfieldvalidator yourself. Something like:

    <asp:TemplateField HeaderText="CostCategory" SortExpression="CostCategory">                
                 <%#Eval("CostCategory") %>

                  <asp:TextBox runat="server" ID="CostCategoryTextBox" Text='<%#Bind("CostCategory") %>' />
                   <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="CostCategoryTextBox" SetFocusOnError="True" Display="Dynamic" ToolTip="Enter a Cost Category" ErrorMessage="The Cost Category field is required " ValidationGroup="GridGroup" />
    LVL 1

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    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

    18 Experts available now in Live!

    Get 1:1 Help Now