sandeep
sandeep

Reputation: 2882

How to validate TextBox Withing GridView Footer using Validator control?

Can anyone please help me to validate Textbox within Footer of the GridView Control upon Clicking the Button

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">


<ContentTemplate>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
        DataKeyNames="Id" ShowFooter="true" onrowdatabound="GridView1_RowDataBound" >
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    Units
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="txt_Units" runat="server" Text='<%# Eval("Units") %>' ToolTip="Enter Units"></asp:TextBox>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ReadOnly="true" Text="999999" Enabled="false" ID="txt_MaxUnits" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <HeaderTemplate>
                    Cost
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="txt_Cost" runat="server" Text='<%# Eval("Cost") %>' ></asp:TextBox>
                </ItemTemplate>
                <FooterTemplate>

                    <asp:TextBox ID="txt_MaxCost" runat="server"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Enter Integer Between 0 to 999998" ControlToValidate="txt_Units" ValidationExpression="^(0{0,5}[1-9]|0?[1-9][0-9]|[1-9][0-9][0-9]| [1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9][0-9])$" ValidationGroup="vld_Rows"></asp:RegularExpressionValidator>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Enter Integer Between 0 to 999998" ControlToValidate="txt_Cost" ValidationExpression="^(0{0,5}[1-9]|0?[1-9][0-9]|[1-9][0-9][0-9]| [1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9][0-9])$" Display="Dynamic" ValidationGroup="vld_Rows"></asp:RegularExpressionValidator>
                </ItemTemplate>
                <FooterTemplate>
                   <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="Enter Integer Between 0 to 999998" ControlToValidate="txt_MaxCost" ValidationExpression="^(0{0,5}[1-9]|0?[1-9][0-9]|[1-9][0-9][0-9]| [1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9][0-9])$"  ValidationGroup="vld_Rows1" ></asp:RegularExpressionValidator> 
                </FooterTemplate>
            </asp:TemplateField>
            <%--<asp:TemplateField>
                <HeaderTemplate>
                    Is Max
                </HeaderTemplate>
                <ItemTemplate>--%>
                   <%-- <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("IsMax") %>'></asp:TextBox>--%>
                   <%-- <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("IsMax") %>' OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="true" />
                </ItemTemplate>
            </asp:TemplateField>--%>

        </Columns>
    </asp:GridView>

    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" ValidationGroup="vld_Rows" />
    <asp:Button ID="btn_Save" runat="server" Text="Save" onclick="btn_Save_Click" ValidationGroup="vld_Rows1" />
    </ContentTemplate>

      <%--<Triggers>
    <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" runat="server" />
    <asp:AsyncPostBackTrigger ControlID="CheckBox1" EventName="CheckedChanged" runat="server" />
</Triggers>--%>

    </asp:UpdatePanel>

Above is the Sample Code Which I have used. Upon Clicking Button(btn_Save) I need to validate the ValidateGroup with id as "vld_Rows1"

Please Suggest a Solution.

Thanks in Advance!

Upvotes: 0

Views: 6002

Answers (2)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

Put the validation control in the same template where you put your control. In your scenario, your controls are in the footer template, so you should put the validation controls in footer template, and try to use validation group as well. e.g.

 <asp:TemplateField>
      <ItemTemplate>
      </ItemTemplate>
      <FooterTemplate>
          <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><asp:RequiredFieldValidator
                        ID="RequiredFieldValidator1" runat="server" ValidationGroup="ft" ControlToValidate="TextBox3"
                        ErrorMessage="*"></asp:RequiredFieldValidator>
       </FooterTemplate>
 </asp:TemplateField>

Upvotes: 1

jdavies
jdavies

Reputation: 12894

Checkout the following MSDN article: RegularExpressionValidator Control

From the article:

If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to prevent the user from skipping an input control.

As it states you will need to make use of a RequiredFieldValidator to ensure that data has been entered.

Upvotes: 1

Related Questions