Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to use a compare Validator to compare two textboxes inside a gridview template. i want to compare one textbox value with the length of other textbox. If both are not same error msg should show. how to do it. please help
Posted
Comments
[no name] 26-Aug-14 8:16am    
Which type of error u occured..?
Member 10578683 26-Aug-14 8:28am    
i don't know how to write a custom validator for the textboes present in gridview template field

if you want to compare values of both textboxes then only you can use compare validator,

if you want to compare value,and length means you can use custom validator

check below link for custom validator
http://asp.net-tutorials.com/validation/custom-validator/[^]
 
Share this answer
 
v2
Comments
Member 10578683 26-Aug-14 8:11am    
how to use custom validator for the textboxes inside gridview
ClimerChinna 26-Aug-14 8:54am    
look at solution2
below is gridview code

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="lbleno" Text='<%#Eval("eno") %>' runat="server"></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox ID="txtename" Text='<%#Eval("ename") %>' runat="server"></asp:TextBox>
                    </ItemTemplate>

                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox ID="txtename1" Text='<%#Eval("ename") %>' runat="server"></asp:TextBox>
                        <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="ename,ename1 must match"
                            OnServerValidate="CustomValidator1_ServerValidate" ValidateEmptyText="true" ValidationGroup="txt"></asp:CustomValidator>
                    </ItemTemplate>

                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="lblsal" Text='<%#Eval("salary") %>' runat="server"></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="btnsubmit" runat="server" Text="Submit" ValidationGroup="txt" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


.cs page code

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection conn = new SqlConnection(your connection string);
            SqlDataAdapter da = new SqlDataAdapter("select * from tablename", conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        CustomValidator cv=(CustomValidator)source;
        GridViewRow gvr = (GridViewRow)cv.NamingContainer;
        TextBox txt1 = (TextBox)gvr.FindControl("txtename");
        TextBox txt2= (TextBox)gvr.FindControl("txtename1"); 
        if(txt1.Text==txt2.Text)
        {
            args.IsValid=true;     
        }
        else
            args.IsValid=false;
    }


i have executed this as ana example you can change it according to your purpose,
condition also you can use your required condition
 
Share this answer
 
Comments
Member 10578683 26-Aug-14 9:27am    
<asp:CustomValidator ID="CustomValidator1" runat="server"
ValidateEmptyText="true" ValidationGroup="txt"
ErrorMessage="please enter correct no. of serial number" OnServerValidate="CustomValidator1_ServerValidate" >


protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{

CustomValidator cv = (CustomValidator)source;
GridViewRow gvr = (GridViewRow)cv.NamingContainer;
CheckBox chk = (CheckBox)gvr.FindControl("CheckBox1");
TextBox txt1 = (TextBox)gvr.FindControl("TextBox1");
TextBox txt2 = (TextBox)gvr.FindControl("TextBox6");
string serials = txt2.Text;
string[] sl = serials.Split(',');
if (chk.Checked == true)
{
if (sl.Length == Convert.ToInt16(txt1.Text))
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
}


Hello i wrote my code like this but it is not working
ClimerChinna 26-Aug-14 9:30am    
what output did you get
Member 10578683 26-Aug-14 9:45am    
this is also not working. i didn't gen anything. it is not comparing
Member 10578683 27-Aug-14 2:16am    
Thanks. It is working now.I forgot to write validation group.
ClimerChinna 27-Aug-14 2:22am    
welcome, happy coding

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900