比较验证器问题 [英] Compare validator problem

查看:60
本文介绍了比较验证器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用比较验证程序比较gridview模板中的两个文本框。我想比较一个文本框值与其他文本框的长度。如果两者都不相同,则应显示msg错误。怎么做。请帮助

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

推荐答案

如果你想比较两个文本框的值,那么只有你可以使用比较验证器,



如果你想比较价值,长度意味着你可以使用自定义验证器



检查下面链接的自定义验证器

http://asp.net-tutorials.com/validation/custom-validator/ [ ^ ]
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/[^]

以下是

gridview代码



below is gridview code

<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页码





.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已经执行了这个例子你可以根据你的目的改变它,

条件也可以使用您所需的条件



i have executed this as ana example you can change it according to your purpose,
condition also you can use your required condition


这篇关于比较验证器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆