加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 使用CompareValidator控件将用户输入日期与今天的日

发布时间:2020-12-15 19:26:16 所属栏目:asp.Net 来源:网络整理
导读:hey..i想比较当前的日期与用户输入的日期.无论如何,我遇到错误到目前为止.. 我试过这样的东西: asp:TextBox id="txtDate1" runat="server" / asp:CompareValidator runat="server" ErrorMessage="The date must be greater than today" ControlToValidate="
hey..i想比较当前的日期与用户输入的日期.无论如何,我遇到错误到目前为止..

我试过这样的东西:

<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()" />

我收到一个错误,指出ValueToCompare属性的“Date”的DateTime.Today.ToShortDateString()值不能转换为类型’date’
我也尝试过ValueToCompare =“DateTime.Now.Date()”,我得到相同的错误信息.

请帮助我,我非常感谢.

解决方法

您只是使用ValueToCompare属性作为文字字符串.如果要执行代码以获取动态值,则需要在其中使用ASP标签.尝试这个:
<asp:comparevalidator runat="server" 
  errormessage="The date must be greater than today"
  controltovalidate="txtDate1" type="date" 
  valuetocompare="<%# DateTime.Today.ToShortDateString() %>" />

然后在您的Page_Load方法中,调用Page.DataBind().

这将在页面加载时执行数据库代码,并将值放在引号之间.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读