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

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

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
indyngAuthor Commented:

How can a text box in the gridview be validated?
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" />

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
indyngAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.