Hel*_*oBD 11 .net asp.net validation comparevalidator
嘿..我想将当前日期与用户输入的日期进行比较..但是,到目前为止,我遇到了错误..
我试过这样的事情:
<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()" />
Run Code Online (Sandbox Code Playgroud)
我得到一个错误,指出"" DateTime.Today.ToShortDateString()的ValueToCompare属性的值不能转换为类型'日期'我也尝试过ValueToCompare="DateTime.Now.Date()",我得到了相同的错误消息.
请帮助我,我非常感谢.
wom*_*omp 17
您只是将该ValueToCompare属性用作文字字符串.如果要执行代码以获取动态值,则需要在其中使用ASP标记.试试这个:
<asp:comparevalidator runat="server"
errormessage="The date must be greater than today"
controltovalidate="txtDate1" type="date"
valuetocompare="<%# DateTime.Today.ToShortDateString() %>" />
Run Code Online (Sandbox Code Playgroud)
然后在你的Page_Load方法中,打电话Page.DataBind().
这将在加载页面时执行数据库代码,并将值放在引号之间.
<asp:CompareValidator ID="CompareValidator3" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Date should be on or after today"
Operator="GreaterThanEqual" Type="Date">
</asp:CompareValidator>
Run Code Online (Sandbox Code Playgroud)
在页面加载事件中,将验证器的值设置为比较为
CompareValidator3.ValueToCompare = DateTime.Now.ToShortDateString();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
64606 次 |
| 最近记录: |