Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 比较验证器?还是自定义验证器?_C#_Asp.net_Requiredfieldvalidator_Customvalidator_Comparevalidator - Fatal编程技术网

C# 比较验证器?还是自定义验证器?

C# 比较验证器?还是自定义验证器?,c#,asp.net,requiredfieldvalidator,customvalidator,comparevalidator,C#,Asp.net,Requiredfieldvalidator,Customvalidator,Comparevalidator,我有两个ASP.NET文本框 文本框1 文本框2 如果用户在文本框1中输入任何内容,我希望输入文本框2是必需的。 实现这一点的最佳方法是什么 比色器? RequiredValidator? 客户验证器 这是我的密码: <div class="MHStransactionDateFrom"> <asp:Label ID="lblMHSTransactionDateFrom" runat="server" Text="Transact

我有两个ASP.NET文本框

文本框1 文本框2

如果用户在文本框1中输入任何内容,我希望输入文本框2是必需的。 实现这一点的最佳方法是什么

比色器? RequiredValidator? 客户验证器

这是我的密码:

            <div class="MHStransactionDateFrom">
            <asp:Label ID="lblMHSTransactionDateFrom" runat="server" Text="Transaction Date From" Width="120"></asp:Label>
            <asp:TextBox ID="ddlMHSTransactonDateFrom" runat="server" Height="10px" Width="100px" CssClass="small date"></asp:TextBox>

        <div class="MHStransactionDateTo">
            <asp:Label ID="lblMHSTransactionDateTo" runat="server" Text="Transaction Date To" style="font-size: 1.2em; color: #FFF;" Width="200"></asp:Label>
            <asp:TextBox ID="ddlMHSTransactionDateTo" runat="server" Height="10px" Width="100px" CssClass="small date"></asp:TextBox>

                        </div>  

在我看来,完美的选择是相对于您想要做的事情,如果第二个文本框必须与第一个文本框具有相同的数据,您应该使用CompareValidor。 如果只需要第二个数据,而不需要与第一个数据相同,则应在此处使用RequiredValidator textbox 2=将验证程序与textbox 1进行比较
自定义也可以使用。但是,如果控件已经为您提供了在TextBox1中输入任何内容时需要输入TextBox2的信息,那么为什么要重新发明轮子呢?你可以在你的代码后面这样做。比如:

 if(TextBox1.Text.Length > 0)
 {
    if(TextBox2.Text.Length == 0)
    {
       //insert error message here
    }
    else
    {
       //do work here
    }

 }

使用验证器的部分好处是,它们可以发出JS来防止表单提交错误数据。