RangeValidator 中的日期格式

Date format in RangeValidator(RangeValidator 中的日期格式)

本文介绍了RangeValidator 中的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RangeValidator 验证在文本框中输入的日期,并且它在默认日期格式下工作正常,但现在我想要dd/MM/yyy"中的日期格式,但它会使用这种日期格式生成 excption.请给我解决方案我的代码:

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:

在 aspx 页面中:

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 >

在代码隐藏中:

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");
}

推荐答案

MinimumValue &最大值需要在 Page_PreRender 事件中设置,并且似乎需要日期格式为dd-MM-yy"...请参阅 Rangevalidator 最小值最大值错误

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

protected void Page_PreRender(object sender, EventArgs e)
{
    RangeValidator1.MinimumValue = DateTime.Now.Date.ToString("dd-MM-yy");
    RangeValidator1.MaximumValue = DateTime.Now.Date.AddYears(90).ToString("dd-MM-yy");
}

这篇关于RangeValidator 中的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:RangeValidator 中的日期格式

基础教程推荐