根据另一个文本框中的值将最小日期设置为文本框类型=日期

时间:2016-04-01 03:47:34

标签: c# asp.net date textbox

我有textbox,其类型为date,我想将所选日期与我的ASP.NET页面中文本框的值进行比较

见下图

enter image description here

ASP.NET代码:(对于日期文本框)

<asp:Label runat="server" ID="Label11" 
    Font-Bold="True"> Date Received:</asp:Label>&nbsp;
<asp:TextBox runat="server" ID="txtDate" 
    Width="150px" type="date" Height="31px" ></asp:TextBox>

代码背后:

txtDate.Attributes.Add("min", DateTime.Now.ToString("yyyy-MM-dd"));

我能够使之前的日期无法选择,但我想比较在最终交割日期选择的日期。

我只是不知道如何。

请帮忙。先感谢您。 ASP.NET和C#还是新手。

1 个答案:

答案 0 :(得分:0)

您应该能够使用Asp.Net的比较验证器实现此目的。

<asp:CompareValidator ID="cmpVal1" ControlToCompare="txtFinalDeliveryDate" 
     ControlToValidate="txtDate" Type="Date" Operator="LessThanEqual"   
     ErrorMessage="Date should be less than final delivery date"
     runat="server"></asp:CompareValidator> 

如果用户输入的日期大于最终交付日期,则会显示错误消息。

仅当两个控件都是输入控件时才会起作用。如果需要与标签进行比较,则可以使用ValueToCompare属性而不是ControlToCompare。 ValueToCompare可以从后面的代码设置