AjaxToolkit:CalendarExtender 控件,可以让用户选择日期。
由于不能对它做过多的定制,但我们可以使用另外的方法实现相同的功能。如设定用户输入的日期格式,判断用户输入的值是否为日期,使用asp:CompareValidator限制用户输入的日期等于、小于,小于等于、大于或大于等于某一个锚点日期。
View Code
日期:<asp:TextBox ID="txtDespatchDate" runat="server" CssClass="textbox" Width="80px"></asp:TextBox>
<!--日历小图标-->
<img id="img1" src='<%= ResolveUrl("~/Calendar.gif")%>' border="0" />
<!--Ajax Calendar 控件,可以设定日期格式-->
<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDespatchDate"
PopupButtonID="img1" Format="yyyy-MM-dd">
</ajaxToolkit:CalendarExtender>
<!--判断日期栏位必须填定-->
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="txtDespatchDate"
Display="none" ErrorMessage="日期栏位,必须填写!" ValidationGroup="vg1"></asp:RequiredFieldValidator>
<!--判断用户输入的值是否为日期-->
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtDespatchDate"
Operator="DataTypeCheck" Type="date" Display="None" ErrorMessage="日期栏位,非有效日期!"
ValidationGroup="vg1"></asp:CompareValidator>
<!--比较用户输入的日期,是否小于等于当天-->
<asp:CompareValidator ID="CompareValidatorDespatchDate" runat="server" ControlToValidate="txtDespatchDate"
Display="None" ErrorMessage="日期栏位,只能填写过去或是今天的日期!" Operator="LessThanEqual"></asp:CompareValidator>
<!--日历小图标-->
<img id="img1" src='<%= ResolveUrl("~/Calendar.gif")%>' border="0" />
<!--Ajax Calendar 控件,可以设定日期格式-->
<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDespatchDate"
PopupButtonID="img1" Format="yyyy-MM-dd">
</ajaxToolkit:CalendarExtender>
<!--判断日期栏位必须填定-->
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="txtDespatchDate"
Display="none" ErrorMessage="日期栏位,必须填写!" ValidationGroup="vg1"></asp:RequiredFieldValidator>
<!--判断用户输入的值是否为日期-->
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtDespatchDate"
Operator="DataTypeCheck" Type="date" Display="None" ErrorMessage="日期栏位,非有效日期!"
ValidationGroup="vg1"></asp:CompareValidator>
<!--比较用户输入的日期,是否小于等于当天-->
<asp:CompareValidator ID="CompareValidatorDespatchDate" runat="server" ControlToValidate="txtDespatchDate"
Display="None" ErrorMessage="日期栏位,只能填写过去或是今天的日期!" Operator="LessThanEqual"></asp:CompareValidator>
比较日期,还需要在Page的Page_load事件中写:
cs:
View Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//以下有一个对象,可参考:http://www.cnblogs.com/insus/articles/1735126.html
{
if (!IsPostBack)
{
//以下有一个对象,可参考:http://www.cnblogs.com/insus/articles/1735126.html
CompareValidatorDespatchDate.ValueToCompare = objInsusDateTimeUtility.GetDateTime(DateTime.Today, "yyyy-MM-dd");
}
}
}
}
vb.net:
View Code
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Data_Binding()
'以下有一个对象,可参考:http://www.cnblogs.com/insus/articles/1735126.html
CompareValidatorDespatchDate.ValueToCompare = objInsusDateTimeUtility.GetDateTime(DateTime.Today, "yyyy-MM-dd")
End If
End Sub
If Not IsPostBack Then
Data_Binding()
'以下有一个对象,可参考:http://www.cnblogs.com/insus/articles/1735126.html
CompareValidatorDespatchDate.ValueToCompare = objInsusDateTimeUtility.GetDateTime(DateTime.Today, "yyyy-MM-dd")
End If
End Sub
See also:
http://www.cnblogs.com/insus/archive/2011/08/27/2155687.html