• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

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

0
indyng
Asked:
indyng
  • 2
  • 2
1 Solution
 
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
 
indyngAuthor Commented:
awesome!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now