解决方法
How to make sure user does not select a date earlier than today or greater than today
可能存在您不希望用户选择比当前日期早一天的情况.例如:当您向用户提供表单以预订票证时,您不希望他选择较早的日期.要达到此要求,请使用以下JavaScript代码.
Prevent the User from selecting a Date Earlier than today
<head runat="server"> <title>Calendar Extender</title> <script type="text/javascript"> function checkDate(sender,args) { if (sender._selectedDate < new Date()) { alert("You cannot select a day earlier than today!"); sender._selectedDate = new Date(); // set the date back to the current date sender._textBox.set_Value(sender._selectedDate.format(sender._format)) } } </script> </head>
<form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <cc1:CalendarExtender ID="CalendarExtender1" runat="server" OnClientDateSelectionChanged="checkDate" TargetControlID="TextBox1" /> </div> </form>
Select Date Greater than today
在javascript中,只需更改此行即可
sender._selectedDate>新日期()
注意:您可能会争辩说,用户仍然可以通过在文本框中输入或输入无效日期来更改日期.那么可以使用ValidationControl轻松处理,并在下一篇技巧中介绍.
Add validation to the CalendarExtender Control
向Calendar添加验证的一种简单方法是将ValidationControl添加到与CalendarExtender关联的文本框中.你有两个选择:
>将Extender添加到ValidationControl.为此,请拖放ValidationControl>点击ValidationControl的智能标签>选择添加扩展器.从Extender向导中,选择ValidatorCalloutExtender.使用此方法可以非常轻松地将控件扩展器发现并附加到控件上.在VS 2005中,您必须通过连接控制扩展器手动执行此过程.
>您可以选择不添加扩展器.
我们将继续使用选项A.我们将向TextBox添加两个ValidationControl.第一个是CompareValidator,用于检查用户是否未输入无效日期(例如:5月32日),第二个是RangeValidator,以根据需要保留日期范围.
添加CompareValidator
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlTovalidate="TextBox1" display="Dynamic" ErrorMessage="Invalid Date" Operator="DataTypeCheck" Type="Date"> </asp:CompareValidator> <cc1:ValidatorCalloutExtender ID="CompareValidator1_ValidatorCalloutExtender" runat="server" Enabled="True" TargetControlID="CompareValidator1"> </cc1:ValidatorCalloutExtender> Adding RangeValidator – We will restrict the user to select a date range starting from today to 15 days from Now. <asp:RangeValidator ID="RangeValidator1" runat="server" ControlTovalidate="TextBox1" ErrorMessage="RangeValidator" Type="Date"> </asp:RangeValidator> <cc1:ValidatorCalloutExtender ID="RangeValidator1_ValidatorCalloutExtender" runat="server" Enabled="True" TargetControlID="RangeValidator1"> </cc1:ValidatorCalloutExtender>
protected void Page_Load(object sender,EventArgs e) { RangeValidator1.MinimumValue = System.DateTime.Now.ToShortDateString(); RangeValidator1.MaximumValue = System.DateTime.Now.AddDays(15).ToShortDateString(); }
VB.NET
Protected Sub Page_Load(ByVal sender As Object,ByVal e As EventArgs) RangeValidator1.MinimumValue = System.DateTime.Now.ToShortDateString() RangeValidator1.MaximumValue = System.DateTime.Now.AddDays(15).ToShortDateString() End Sub
那些是与CalendarExtender相关的一些提示.随着工具包的未来版本的发布,我们应该希望能够以更简单的方式实现此功能.
来自:http://www.dotnetcurry.com/ShowArticle.aspx?ID=149
另一种高级方法是扩展CalendarExtender javascript,但是你有自己的自定义版本的ajax工具包.
http://codegoeshere.blogspot.com/2007/06/extending-calendarextender.html