locked
How to validate each textbox in GridView using CustomValidator RRS feed

  • Question

  • User1577504422 posted

    I have GridView with TextBoxes.

    My codes are:
                        <asp:TemplateField HeaderText="Sale Type" Visible="true" ItemStyle-Width="80px">
                            <ItemTemplate>
                                <asp:TextBox runat="server" ID="tb_saleType" Text='<%#DataBinder.Eval(Container.DataItem, "sale_type")%>' CssClass="form-control input-sm"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        

    <asp:TemplateField HeaderText="Internal Product Details">
         <ItemTemplate>
              <asp:TextBox ID="tb_prodCode" runat="server" CssClass="form-control input-sm" Text='<%#DataBinder.Eval(Container.DataItem,  "ConvertProd")%>'></asp:TextBox>
    <asp:CustomValidator ID="cv_tb_prodCode" runat="server" ControlToValidate="tb_prodCode" ForeColor="Tomato" Display="Dynamic" ErrorMessage="Product code does not exists" SetFocusOnError="true" OnServerValidate="cv_tb_prodCode_ServerValidate"></asp:CustomValidator>
         </ItemTemplate>
    </asp:TemplateField>

    c# :


        protected void cv_tb_prodCode_ServerValidate(object source, ServerValidateEventArgs args)
        {
            TextBox saleType = gridProducts.Rows[gridProducts.EditIndex].FindControl("tb_saleType") as TextBox;
            TextBox prodCode = gridProducts.Rows[gridProducts.EditIndex].FindControl("tb_prodCode") as TextBox;

            //to validate if product code planned to convert //check if exists in db
            string prodDescx = new DAL.ePlannings().getProdCodeDesc(saleType.Text.Trim(), prodCode.Text.Trim(), tb_mfg.Text);

            if (prodDescx == null)
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }

    I need to validate those textboxes once user click submit. But come out error on method cv_tb_prodCode_ServerValidate

    Thursday, April 1, 2021 6:30 AM

All replies

  • User1535942433 posted

    Hi deannaxcv,

    According to your description,I have tested your codes and I reproduced your problems.

    When you click the button to validate the value,it couldn't find which rows of gridview you have clicked. So you need to get the index you have clicked.Just like this:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField HeaderText="Sale Type" Visible="true" ItemStyle-Width="80px">
                            <ItemTemplate>
                                <asp:TextBox runat="server" ID="tb_saleType" Text='<%#DataBinder.Eval(Container.DataItem, "Id")%>' CssClass="form-control input-sm"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Internal Product Details">
                            <ItemTemplate>
                                <asp:TextBox ID="tb_prodCode" runat="server" CssClass="form-control input-sm" Text='<%#DataBinder.Eval(Container.DataItem,  "Total")%>'></asp:TextBox>
                                <asp:CustomValidator ID="cv_tb_prodCode" runat="server" ControlToValidate="tb_prodCode" ForeColor="Tomato" Display="Dynamic" ErrorMessage="Product code does not exists" SetFocusOnError="true" OnServerValidate="cv_tb_prodCode_ServerValidate"></asp:CustomValidator>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

    Code-behind:

    protected void cv_tb_prodCode_ServerValidate(object source, ServerValidateEventArgs args)
            {
                CustomValidator cv = (CustomValidator)source;
                GridViewRow row = (GridViewRow)cv.NamingContainer;
                TextBox saleType = row.FindControl("tb_saleType") as TextBox;
                TextBox prodCode = row.FindControl("tb_prodCode") as TextBox;
                //to validate if product code planned to convert //check if exists in db
                string prodDescx = new DAL.ePlannings().getProdCodeDesc(saleType.Text.Trim(), prodCode.Text.Trim(), tb_mfg.Text);
                if (prodDescx == null)
                {
                   args.IsValid = false;
                }
                else
                {
                   args.IsValid = true;
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
    
            }

    Best regards,

    Yijing Sun

    Friday, April 2, 2021 3:06 AM