如何通过其渲染事件禁用 CalendarExtender 控件中的先前日期? [英] how to disable previous dates in CalendarExtender control through its render event?

查看:25
本文介绍了如何通过其渲染事件禁用 CalendarExtender 控件中的先前日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我只想允许选择大于今天的日期.我更喜欢这种方式来避免显示警报消息.

Basically, I just want allow select dates greater than today. I'd prefer this way to avoid show alert messages.

推荐答案

我认为当前版本的 Toolkit 不支持限制可选日期.这是处理 ClientDateSelectedChanged-Event 并验证所选日期的简单解决方法:

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

在某些情况下,您可能不希望用户选择比当前日期早的一天.例如:当您为用户提供预订机票的表单时,您不希望他选择更早的日期.要实现此要求,请使用以下 javascript 代码.

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>

调用代码:

<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 轻松处理,并在下一个技巧中介绍.

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.

向 CalendarExtender 控件添加验证

Add validation to the CalendarExtender Control

向 Calendar 添加验证的一种简单方法是将 ValidationControl 添加到与 CalendarExtender 关联的文本框.你有两个选择:

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:

  1. Extender 添加到 ValidationControl.为此,拖放 ValidationControl > 单击 ValidationControl 的智能标记 > 选择 Add Extender.从扩展程序向导中,选择 ValidatorCalloutExtender.使用这种方法可以非常容易地发现控件扩展器并将其附加到您的控件上.在 VS 2005 中,您必须通过连接控制扩展器来手动执行此过程.
  2. 您可以选择不添加扩展器.我们将继续执行选项 A.我们将向 TextBox 添加两个 ValidationControls.第一个是 CompareValidator 来检查用户是否没有输入无效日期(例如:5 月 32 日),第二个是 RangeValidator 以保持所需的日期范围.
  1. Add an Extender to the ValidationControl. To do so, drag and drop a ValidationControl > click on the smart tag of the ValidationControl > choose Add Extender. From the Extender Wizard, choose ValidatorCalloutExtender. 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.
  2. You can choose not to add the Extender. We will go ahead with option A. We will be adding two ValidationControls to the TextBox. The first, a CompareValidator to check if the user does not enter an invalid date (Eg: May 32) and second, a RangeValidator to keep the date range as desired.

添加比较验证器

<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>

在页面后面的代码中,添加此代码C#

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

好吧,这些是与 CalendarExtender 相关的一些技巧.随着工具包未来版本的发布,我们应该希望有更简单的方法来实现此功能.

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.

来自:http://www.dotnetcurry.com/ShowArticle.aspx?ID=149

另一种高级方法是扩展 CalendarExtender javascript,但是您有自己的 ajax 工具包的自定义版本.

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 控件中的先前日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆