RangeValidator 中的日期格式
我正在使用 RangeValidator 来验证文本框中输入的日期,并且它在默认日期格式下工作正常,但现在我想要“dd/MM/yyy”中的日期格式,但它使用此日期格式生成例外。请为我提供解决方案 我的代码:
在 aspx 页面中:
<asp:TextBox ID="txtrequiredby" runat="server" ></asp:TextBox >
<cc1:CalendarExtender ID="txtrequiredby_CalendarExtender" Format="dd/MM/yyyy"
runat="server" Enabled="True" TargetControlID="txtrequiredby" >
</cc1:CalendarExtender >
<asp:RangeValidator ID="rvreqby" runat="server" ErrorMessage="Required By Date
Greater Than or Equal to current date" ControlToValidate="txtrequiredby"
Display="Dynamic" Type="Date" ></asp:RangeValidator >
在代码隐藏中:
rvreqby.MinimumValue = clsGeneral.FromSqlDate( DateTime.Now);
rvreqby.MaximumValue = clsGeneral.FromSqlDate( DateTime.Now.AddYears(200));
public static string FromSqlDate(DateTime date)
{
return date.ToString("dd/MM/yyyy");
}
I am using RangeValidator to validate date enter in textbox and its working fine with default date format but now i want the date format in "dd/MM/yyy" but its generating excption with this date format. please provide me solution
my code:
in aspx page:
<asp:TextBox ID="txtrequiredby" runat="server" ></asp:TextBox >
<cc1:CalendarExtender ID="txtrequiredby_CalendarExtender" Format="dd/MM/yyyy"
runat="server" Enabled="True" TargetControlID="txtrequiredby" >
</cc1:CalendarExtender >
<asp:RangeValidator ID="rvreqby" runat="server" ErrorMessage="Required By Date
Greater Than or Equal to current date" ControlToValidate="txtrequiredby"
Display="Dynamic" Type="Date" ></asp:RangeValidator >
in codebehind:
rvreqby.MinimumValue = clsGeneral.FromSqlDate( DateTime.Now);
rvreqby.MaximumValue = clsGeneral.FromSqlDate( DateTime.Now.AddYears(200));
public static string FromSqlDate(DateTime date)
{
return date.ToString("dd/MM/yyyy");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最小值和MaximumValue 需要在 Page_PreRender 事件中设置,并且似乎需要日期格式为“dd-MM-yy”...请参阅 范围验证器最小值最大值错误
MinimumValue & MaximumValue need to be set in the Page_PreRender event and appear to require the date format as "dd-MM-yy"...see last post on Rangevalidator Min Max Value error
最小值和最大值的格式应为 yyyy/MM/dd 请
在此处查看文档:
https://msdn.microsoft.com/en-我们/library/ydez7ad7(v=vs.110).aspx
Format of the MinimumValue and MaximumValue should be yyyy/MM/dd
Check documentation here:
https://msdn.microsoft.com/en-us/library/ydez7ad7(v=vs.110).aspx