如何在CalendarExtender控制通过其渲染事件禁用previous日期? [英] how to disable previous dates in CalendarExtender control through its render event?
问题描述
基本上,我只想让选择日期大于今天。我想preFER这种方式来避免显示警告讯息。
我不认为这是在当前版本的工具包来限制可选日期的支持。这是一个简单的解决方法处理 ClientDateSelectedChanged
- 活动和验证所选日期:
如何确保用户不会选择一个日期早于当天或大于今天
块引用>有可能是情况下,您不希望用户选择早于当前日期的一天。例如:当您为用户提供一个表单订票,你不会喜欢他选择一个较早的日期。为了达到这一要求,可以使用下面的JavaScript code。
prevent从选择的日期早于当天用户
块引用><头=服务器>
<标题>日历扩展< /标题>
<脚本类型=文/ JavaScript的> 功能了checkdate(发件人,参数)
{
如果(sender._selectedDate<新的Date())
{
警报(你不能选择早一天比今天!);
sender._selectedDate =新的日期();
//设置日期回当前日期
sender._textbox.set_Value(sender._selectedDate.format(sender._format))
}
}
< / SCRIPT>
< /头>调用code:
<表ID =form1的=服务器>
< ASP:的ScriptManager ID =ScriptManager1=服务器/>
< DIV> < ASP:文本框ID =TextBox1的=服务器>< / ASP:文本框>
&所述; CC1:CalendarExtender ID =CalendarExtender1
=服务器OnClientDateSelectionChanged =了checkdate的TargetControlID =TextBox1中/> < / DIV>
< /表及GT;
选择日期大于今天
块引用>在javascript的,只是改变这一行
sender._selectedDate>新的Date()
注意:您可能认为,用户仍然可以通过输入到文本框中输入或无效的日期更改日期。好了,可以使用ValidationControl好办,覆盖在接下来的提示。
添加验证到CalendarExtender控制
块引用>有一个简单的方法来添加验证的日历是一个ValidationControl添加到与CalendarExtender相关的文本框。你有两个选择:
- 的
延长
添加到ValidationControl
。要做到这一点,拖放ValidationControl
>点击ValidationControl
>选<$ C $的智能标签C>添加扩展。从扩展向导,选择ValidatorCalloutExtender
。使用这种方法使得它非常容易发现和连接控制扩展到您的控件。在VS 2005中,您必须手动执行此操作过程中,连接最多控制扩展。- 您可以选择不添加扩展。
我们将与选项A:我们将增加两个ValidationControls
到文本框
继续。第一,一个CompareValidator
来检查,如果用户没有输入无效日期(例如:五月32)和第二,一个RangeValidator控件
以保留日期范围内所需。添加CompareValidator
&LT; ASP:CompareValidator ID =CompareValidator1=服务器
的ControlToValidate =TextBox1的显示=动态的ErrorMessage =无效的日期
操作=DataTypeCheck类型=日期&GT;
&LT; / ASP:CompareValidator&GT;
&所述; CC1:ValidatorCalloutExtender ID =CompareValidator1_ValidatorCalloutExtender
=服务器启用=真的TargetControlID =CompareValidator1&GT;
&LT; / CC1:ValidatorCalloutExtender&GT;
添加RangeValidator控件 - 我们将限制用户选择从今天从现在开始到15天的日期范围。
&LT; ASP:RangeValidator控件ID =RangeValidator1=服务器
的ControlToValidate =TextBox1中的ErrorMessage =RangeValidator控件
TYPE =日期&GT;
&LT; / ASP:RangeValidator控件&GT;
&所述; CC1:ValidatorCalloutExtender ID =RangeValidator1_ValidatorCalloutExtender
=服务器启用=真的TargetControlID =RangeValidator1&GT;
&LT; / CC1:ValidatorCalloutExtender&GT;在您的网页背后的code,添加此code
C#保护无效的Page_Load(对象发件人,EventArgs的发送)
{
RangeValidator1.MinimumValue = System.DateTime.Now.ToShortDateString();
RangeValidator1.MaximumValue = System.DateTime.Now.AddDays(15).ToShortDateString();
}VB.NET
保护小组的Page_Load(BYVAL发件人为对象,BYVAL E上的EventArgs)
RangeValidator1.MinimumValue = System.DateTime.Now.ToShortDateString()
RangeValidator1.MaximumValue = System.DateTime.Now.AddDays(15).ToShortDateString()
结束小组那么这些均与
CalendarExtender
相关联的一些提示。由于该工具包的未来版本发布时,我们应该希望,将存在更简单的方法,要实现这个功能的。从: http://www.dotnetcurry.com /ShowArticle.aspx?ID=149
另一种先进的方法是延长CalendarExtender JavaScript的,但你有Ajax工具包的自定义的版本。
HTTP://$c$cgoeshere.blogspot.com/ 2007/06 /延长-calendarextender.html
Basically, I just want allow select dates greater than today. I'd prefer this way to avoid show alert messages.
解决方案I don't think that it is supported in the current version of the Toolkit to restrict selectable dates. This is a simple workaround handling the
ClientDateSelectedChanged
-Event and validate the selected date:How to make sure user does not select a date earlier than today or greater than today
There could be instances where you do not want the user to select a day earlier than the current date. For example: when you are providing the user a form to book tickets, you would not like him to choose an earlier date. To achieve this requirement, use the following javascript code.
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>
Call the code:
<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
In the javascript, just change this line
sender._selectedDate > new Date()
Note: You may argue that the user can still change the date by typing into the textbox or entering an invalid date. Well that can be easily handled using a ValidationControl and is covered in the next tip.Add validation to the CalendarExtender Control
A simple way to add validation to the Calendar is to add a ValidationControl to the textbox associated with a CalendarExtender. You have two choices:
- Add an
Extender
to theValidationControl
. To do so, drag and drop aValidationControl
> click on the smart tag of theValidationControl
> chooseAdd Extender
. From the Extender Wizard, chooseValidatorCalloutExtender
. Using this approach makes it extremely easy to discover and attach control extenders to your controls. In VS 2005, you had to do this process manually, by wiring up control extenders.- You can choose not to add the Extender. We will go ahead with option A. We will be adding two
ValidationControls
to theTextBox
. The first, aCompareValidator
to check if the user does not enter an invalid date (Eg: May 32) and second, aRangeValidator
to keep the date range as desired.Adding 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>
In the code behind of your page, add this code C#
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
Well those were some tips associated with the
CalendarExtender
. As future versions of the toolkit are released, we should be hopeful that there will exist easier ways, of achieving this functionality.From: http://www.dotnetcurry.com/ShowArticle.aspx?ID=149
Another advanced approach would be to extend the CalendarExtender javascript, but then you have your own custom version of the ajax toolkit.
http://codegoeshere.blogspot.com/2007/06/extending-calendarextender.html
这篇关于如何在CalendarExtender控制通过其渲染事件禁用previous日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!