禁用 ajaxToolkit CalendarExtender 中的先前日期

发布于 2024-07-18 07:26:25 字数 51 浏览 2 评论 0原文

如何在 ajaxToolkit CalendarExtender 中使用时禁用以前的日期

How to disable previous dates while using in ajaxToolkit CalendarExtender

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

dawn曙光 2024-07-25 07:26:25

一种选择是在 calenderextender 绑定到的文本框上使用范围验证器。 即,如果您将日历扩展器的 TargetID 设置为 tb1,请添加一个 rangeValidator 以标记 tb1 的内容是否在今天之前。

另一种选择是使用 javascript,这是一个很好的例子:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=149 提示 6。

One Option is to use a rangevalidator on the textbox the calenderextender is bound to. Ie if you have the TargetID of the calendar extender set to tb1 add a rangeValidator to flag when the contents of tb1 is before today.

Another option is using javascript and here is a good example:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=149 TIP 6.

遗忘曾经 2024-07-25 07:26:25

以下是我对日历日期限制问题的完整解决方案: 我喜欢此解决方案的一点是,您可以设置 RangeValidator 的最小值和最大值,并且无需修改任何 javascript。 我从未找到不需要重新编译 AjaxControlToolkit.dll 的完整解决方案。 感谢 http://www.karpach.com/ajaxtoolkit-calendar-extender-tweaks .htm 让我了解如何重写 calendar.js 文件中的关键方法,而无需重新编译 AjaxControlToolkit.dll。 另外,我收到“AjaxControlToolkit is undefined”javascript 错误,因此我将其更改为 Sys.Extended.UI。 当我使用 4.0 版本的工具包时,它对我有用。

<%--//ADD THIS NEW STYLE TO STYLESHEET TO GRAY OUT DATES THAT AREN'T SELECTABLE--%>
<style type="text/css"> 
    .ajax__calendar_inactive  {color:#dddddd;}
</style>

在 Page_Load 或 Init 中或任何地方,设置范围验证器的最小值和最大值:

<script runat="server">
    protected override void OnLoad(EventArgs e)
    {
        //set the validator min and max values
        this.valDateMustBeWithinMinMaxRange.MinimumValue = DateTime.Today.Date.ToShortDateString();
        this.valDateMustBeWithinMinMaxRange.MaximumValue = DateTime.MaxValue.Date.ToShortDateString();
        base.OnLoad(e);
    }
</script>

在页面中的某处添加此 javascript:

<script type="text/javascript">
<%--//   ADD DATE RANGE FEATURE JAVASCRIPT TO OVERRIDE CALENDAR.JS--%>
        var minDate = new Date('<%= valDateMustBeWithinMinMaxRange.MinimumValue %>');
         var maxDate = new Date('<%= valDateMustBeWithinMinMaxRange.MaximumValue %>');
        Sys.Extended.UI.CalendarBehavior.prototype._button_onblur_original = Sys.Extended.UI.CalendarBehavior.prototype._button_onblur;
        //override the blur event so calendar doesn't close
        Sys.Extended.UI.CalendarBehavior.prototype._button_onblur = function (e) {
            if (!this._selectedDateChanging) {
                this._button_onblur_original(e);
            }
        }
        Sys.Extended.UI.CalendarBehavior.prototype._cell_onclick_original = Sys.Extended.UI.CalendarBehavior.prototype._cell_onclick;
        //override the click event
        Sys.Extended.UI.CalendarBehavior.prototype._cell_onclick = function (e) {
            var selectedDate = e.target.date;

            if (selectedDate < minDate || selectedDate > maxDate ) {
                //alert('Do nothing. You can\'t choose that date.');
                this._selectedDateChanging = false;
                return;
            }

            this._cell_onclick_original(e);
        }

        Sys.Extended.UI.CalendarBehavior.prototype._getCssClass_original = Sys.Extended.UI.CalendarBehavior.prototype._getCssClass;
        Sys.Extended.UI.CalendarBehavior.prototype._getCssClass = function (date, part) {

            var selectedDate = date;

           if (selectedDate < minDate || selectedDate > maxDate ) {
                return "ajax__calendar_inactive";
            }
            this._getCssClass_original(date, part);
        }

</script>

使用 CalendarExtenter 和 RangeValidator 将此文本框添加到您的 asp.net 页面:

<asp:TextBox ID="textBoxDate" runat="server" />
<ajaxToolkit:CalendarExtender ID="calendarExtender" runat="server" TargetControlID="textBoxDate" />
<asp:RangeValidator ID="valDateMustBeWithinMinMaxRange" runat="server" ControlToValidate="textBoxDate"
    ErrorMessage="The date you chose is not in accepted range" Type="Date" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />

Here is my full solution to the calendar date restriction problem: What I like about this solution is that you set the MinimumValue and MaximumValue of a RangeValidator and you do not have to modify any javascript. I never found a full solution that did not require recompiling the AjaxControlToolkit.dll. Thanks to http://www.karpach.com/ajaxtoolkit-calendar-extender-tweaks.htm for giving me the idea of how to override key methods in the calendar.js file without having to recompile the AjaxControlToolkit.dll. Also, I got "AjaxControlToolkit is undefined" javascript errors, so I changed those to Sys.Extended.UI. and it works for me when using the 4.0 version of the toolkit.

<%--//ADD THIS NEW STYLE TO STYLESHEET TO GRAY OUT DATES THAT AREN'T SELECTABLE--%>
<style type="text/css"> 
    .ajax__calendar_inactive  {color:#dddddd;}
</style>

Either in Page_Load or Init or wherever, set the min and max values for your range validator:

<script runat="server">
    protected override void OnLoad(EventArgs e)
    {
        //set the validator min and max values
        this.valDateMustBeWithinMinMaxRange.MinimumValue = DateTime.Today.Date.ToShortDateString();
        this.valDateMustBeWithinMinMaxRange.MaximumValue = DateTime.MaxValue.Date.ToShortDateString();
        base.OnLoad(e);
    }
</script>

Add this javascript somewhere in your page:

<script type="text/javascript">
<%--//   ADD DATE RANGE FEATURE JAVASCRIPT TO OVERRIDE CALENDAR.JS--%>
        var minDate = new Date('<%= valDateMustBeWithinMinMaxRange.MinimumValue %>');
         var maxDate = new Date('<%= valDateMustBeWithinMinMaxRange.MaximumValue %>');
        Sys.Extended.UI.CalendarBehavior.prototype._button_onblur_original = Sys.Extended.UI.CalendarBehavior.prototype._button_onblur;
        //override the blur event so calendar doesn't close
        Sys.Extended.UI.CalendarBehavior.prototype._button_onblur = function (e) {
            if (!this._selectedDateChanging) {
                this._button_onblur_original(e);
            }
        }
        Sys.Extended.UI.CalendarBehavior.prototype._cell_onclick_original = Sys.Extended.UI.CalendarBehavior.prototype._cell_onclick;
        //override the click event
        Sys.Extended.UI.CalendarBehavior.prototype._cell_onclick = function (e) {
            var selectedDate = e.target.date;

            if (selectedDate < minDate || selectedDate > maxDate ) {
                //alert('Do nothing. You can\'t choose that date.');
                this._selectedDateChanging = false;
                return;
            }

            this._cell_onclick_original(e);
        }

        Sys.Extended.UI.CalendarBehavior.prototype._getCssClass_original = Sys.Extended.UI.CalendarBehavior.prototype._getCssClass;
        Sys.Extended.UI.CalendarBehavior.prototype._getCssClass = function (date, part) {

            var selectedDate = date;

           if (selectedDate < minDate || selectedDate > maxDate ) {
                return "ajax__calendar_inactive";
            }
            this._getCssClass_original(date, part);
        }

</script>

Add this text box to your asp.net page with CalendarExtenter and RangeValidator:

<asp:TextBox ID="textBoxDate" runat="server" />
<ajaxToolkit:CalendarExtender ID="calendarExtender" runat="server" TargetControlID="textBoxDate" />
<asp:RangeValidator ID="valDateMustBeWithinMinMaxRange" runat="server" ControlToValidate="textBoxDate"
    ErrorMessage="The date you chose is not in accepted range" Type="Date" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
深陷 2024-07-25 07:26:25

在 html 标记中使用 Ajax 工具包 Calendar Extender:

<asp:TextBox ID="txtDate" runat="server" CssClass="contentfield" Height="16px" MaxLength="12" width="80px" Wrap="False"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender3" runat="server" Enabled="true" StartDate="<%# DateTime.Now %>" EndDate="<%# DateTime.Now.AddDays(1) %>" Format="dd MMM yyyy" PopupButtonID="imgDatePicker" TargetControlID="txtDate">
</asp:CalendarExtender>
<asp:ImageButton ID="imgDatePicker" runat="Server" AlternateText="Click to show calendar" Height="16px" ImageAlign="Middle" ImageUrl="~/images/Calendar_scheduleHS.png" Width="16px" />

在上面您将看到日历只允许通过设置在今天或明天之间进行选择

StartDate="<%# DateTime.Now %>"

EndDate="<%# DateTime.Now.AddDays(1) %>"

这也可以在后端使用 CalendarExtender1.StartDate = DateTime.Now;CalendarExtender1.EndDate = DateTime.Now.AddDays(1); 完成

Using the Ajax toolkit Calendar Extender in the html markup:

<asp:TextBox ID="txtDate" runat="server" CssClass="contentfield" Height="16px" MaxLength="12" width="80px" Wrap="False"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender3" runat="server" Enabled="true" StartDate="<%# DateTime.Now %>" EndDate="<%# DateTime.Now.AddDays(1) %>" Format="dd MMM yyyy" PopupButtonID="imgDatePicker" TargetControlID="txtDate">
</asp:CalendarExtender>
<asp:ImageButton ID="imgDatePicker" runat="Server" AlternateText="Click to show calendar" Height="16px" ImageAlign="Middle" ImageUrl="~/images/Calendar_scheduleHS.png" Width="16px" />

Above you will see that the Calendar only allows one to choose between today or tomorrow by setting

StartDate="<%# DateTime.Now %>"

and

EndDate="<%# DateTime.Now.AddDays(1) %>"

This can also be done in the backend using CalendarExtender1.StartDate = DateTime.Now; or CalendarExtender1.EndDate = DateTime.Now.AddDays(1);

深爱成瘾 2024-07-25 07:26:25

只需在 ajaxtoolkit calendarextender 控件中添加一个属性 StartDate="<%# DateTime.Now %>"

Just add an attribute StartDate="<%# DateTime.Now %>" in you ajaxtoolkit calendarextender control

夕色琉璃 2024-07-25 07:26:25

以下链接可能对您有帮助:
禁用 CalendarExtender 中的日期

Following link might help you:
Disable dates in CalendarExtender

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文