[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2008-11-04
4
Medium Priority
?
328 Views
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?

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
Comment
Question by:indyng
  • 2
  • 2
4 Comments
 
LVL 15

Expert Comment

by:NazoUK
ID: 22879929
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
 
LVL 1

Author Comment

by:indyng
ID: 22880114
NazoUK:

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

Accepted Solution

by:
NazoUK earned 2000 total points
ID: 22880257
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
 
LVL 1

Author Closing Comment

by:indyng
ID: 31513229
awesome!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

834 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