使用CompareValidator控件与今天的日期比较用户输入日期日期、控件、今天、用户

2023-09-04 23:55:24 作者:Bury.(埋葬)

hey..i想为当前日期与user..however输入的日期相比,我遇到错误为止。

hey..i would like to compare the current date with the date entered by user..however, i'm encountering errors so far..

我想是这样的:

<asp:TextBox id="txtDate1" runat="server" />    
<asp:CompareValidator runat="server" ErrorMessage="The date must be greater than today"
    ControlToValidate="txtDate1" type="date" 
    ValuetoCompare="DateTime.Today.ToShortDateString()" />

和我得到一个错误,指出值 DateTime.Today.ToShortDateString() ValueToCompare 属性的不能转换为类型日期 我也试过 ValueToCompare =DateTime.Now.Date()和我有同样的错误消息。

and i got an error stating that the value of DateTime.Today.ToShortDateString() of the ValueToCompare property of "" cannot be converted to type 'date' i also tried ValueToCompare="DateTime.Now.Date()" and i got the same error message.

请帮我,我非常AP preciate它。

please help me and i greatly appreciate it.

推荐答案

你只是使用 ValueToCompare 属性作为一个字符串。你需要的,如果要执行code以获得一个动态值使用ASP标签了。试试这个:

You're just using the ValueToCompare property as a literal string. You need to use ASP tags in it if you want to execute code to get a dynamic value. Try this:

<asp:comparevalidator runat="server" 
  errormessage="The date must be greater than today"
  controltovalidate="txtDate1" type="date" 
  valuetocompare="<%# DateTime.Today.ToShortDateString() %>" />

然后在你的的Page_Load 的方法,叫的Page.DataBind()

在页面加载这将执行的DataBinder code,并把该值在引号之间。

This will execute the databinder code when the page is loaded, and put the value in between the quotes.