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);
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:
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
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
None
0 Points
4 Posts
How to validate each textbox in GridView using CustomValidator
Apr 01, 2021 06:30 AM|deannaxcv|LINK
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
Contributor
3990 Points
1562 Posts
Re: How to validate each textbox in GridView using CustomValidator
Apr 02, 2021 03:06 AM|yij sun|LINK
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:
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