Disable previous Dates in ajaxToolkit CalendarExtender

前端 未结 5 1758
后悔当初
后悔当初 2020-12-10 06:00

How to disable previous dates while using in ajaxToolkit CalendarExtender

相关标签:
5条回答
  • 2020-12-10 06:21

    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:#ffffdffffd;}
    </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" />
    
    0 讨论(0)
  • 2020-12-10 06:24

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

    0 讨论(0)
  • 2020-12-10 06:25

    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.

    0 讨论(0)
  • 2020-12-10 06:32

    Following link might help you: Disable dates in CalendarExtender

    0 讨论(0)
  • 2020-12-10 06:41

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

    0 讨论(0)
提交回复
热议问题