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?

Thanks
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %>
 
 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <br/>
    <asp:FormView ID="FormView1" runat="server" DataKeyNames="CostCategoryID" DataSourceID="SqlDataSource1"
        DefaultMode="Insert" Width="632px">
        <EditItemTemplate>
            CostCategoryID:
            <asp:Label ID="CostCategoryIDLabel1" runat="server" Text='<%# Eval("CostCategoryID") %>'>
            </asp:Label><br />
            CostCategory:
            <asp:TextBox ID="CostCategoryTextBox" runat="server" Text='<%# Bind("CostCategory") %>'>
            </asp:TextBox><br />
            Description:
            <asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>'>
            </asp:TextBox><br />
            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                Text="Update">
            </asp:LinkButton>
            <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                Text="Cancel">
            </asp:LinkButton>
        </EditItemTemplate>
        <InsertItemTemplate>
            <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"
                Text="Insert"></asp:LinkButton>
            <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                Text="Cancel"></asp:LinkButton>
        </InsertItemTemplate>
        
 
 
        <ItemTemplate>
            CostCategoryID:
            <asp:Label ID="CostCategoryIDLabel" runat="server" Text='<%# Eval("CostCategoryID") %>'></asp:Label><br />
            CostCategory:
            <asp:Label ID="CostCategoryLabel" runat="server" Text='<%# Bind("CostCategory") %>'></asp:Label><br />
            Description:
            <asp:Label ID="DescriptionLabel" runat="server" Text='<%# Bind("Description") %>'></asp:Label><br />
            <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                Text="Edit"></asp:LinkButton>
            <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                Text="Delete"></asp:LinkButton>
            <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
                Text="New"></asp:LinkButton>
        </ItemTemplate>
 
    </asp:FormView>
    <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" />
        <Columns>
            <asp:BoundField DataField="CostCategory" HeaderText="CostCategory" SortExpression="CostCategory" />
            <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
        </Columns>
        <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:GridView>
    <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">
        <DeleteParameters>
            <asp:Parameter Name="CostCategoryID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="CostCategory" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="CostCategory" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
            <asp:Parameter Name="CostCategoryID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
 
</asp:Content>

Open in new window

LVL 1
indyngAsked:
Who is Participating?

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

x
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.

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

How can a text box in the gridview be validated?
0
NazoUKCommented:
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">                
      <ItemTemplate>
             <%#Eval("CostCategory") %>
      </ItemTemplate>

      <EditItemTemplate>
              <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" />
      </EditItemTemplate>
</asp:TemplateField>
0

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:
awesome!
0
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
ASP.NET

From novice to tech pro — start learning today.