Click here to Skip to main content
15,897,128 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I have a calender which works fine,but what I want now is the Calender to show only Month and Year when clicked on it.
What I am doing now is

JavaScript
 <script type="text/javascript">
var textboxStartDate = ["txtbox1"];
function keyDown() {

    var key;
    if (navigator.appName == 'Microsoft Internet Explorer')
        key = event.keyCode;
    else
        key = event.which
    if (key == -1 || key == 8) {

        for (var index = 0; index < textboxStartDate.length; index++) {
            if (textboxStartDate[index] == sender.get_id()) {
                var text = $find(textboxStartDate[index]);
                if (text != null) {
                    document.getElementById(text).value = "";
                }
            }
        }
        event.returnValue = true;
    }
    else {
        alert("Select from Calendar!");
        event.returnValue = false;
    }

</script>
and calling this function on keydown in textbox.This function displays Date, Month and Year. But, I want is only Month and Year.
Thanks
Posted
Updated 15-Jun-12 0:34am
v2
Comments
A_S_A 15-Jun-12 6:42am    
Hi, Manas wanted to know what have you edited.

C#
var cal1;
                  var cal2;

                  function pageLoad() {
                      cal1 = $find("calendar1");
                      cal2 = $find("calendar1");

                      modifyCalDelegates(cal1);
                      modifyCalDelegates(cal2);
                  }

                  function modifyCalDelegates(cal) {
                      //we need to modify the original delegate of the month cell.
                      cal._cell$delegates = {
                          mouseover: Function.createDelegate(cal, cal._cell_onmouseover),
                          mouseout: Function.createDelegate(cal, cal._cell_onmouseout),

                          click: Function.createDelegate(cal, function(e) {
                              ///
                              /// Handles the click event of a cell
                              ///
                              /// The arguments for the event
                              //  e.stopPropagation();
                              e.preventDefault();

                              if (!cal._enabled) return;

                              var target = e.target;
                              var visibleDate = cal._getEffectiveVisibleDate();
                              Sys.UI.DomElement.removeCssClass(target.parentNode, "ajax__calendar_hover");
                              switch (target.mode) {
                                  case "prev":
                                  case "next":
                                      cal._switchMonth(target.date);
                                      break;
                                  case "title":
                                      switch (cal._mode) {
                                          case "days": cal._switchMode("months"); break;
                                          case "months": cal._switchMode("years"); break;
                                      }
                                      break;
                                  case "month":
                                      //if the mode is month, then stop switching to day mode.
                                      if (target.month == visibleDate.getMonth()) {
                                          this._switchMode("days");
                                      }
                                      else {
                                          cal._visibleDate = target.date;
                                          this._switchMode("days");
                                      }
                                      cal.set_selectedDate(target.date);
                                      cal._switchMonth(target.date);
                                      cal._blur.post(true);
                                      cal.raiseDateSelectionChanged();
                                      break;
                                  case "year":
                                      if (target.date.getFullYear() == visibleDate.getFullYear()) {
                                          cal._switchMode("months");
                                      }
                                      else {
                                          cal._visibleDate = target.date;
                                          cal._switchMode("months");
                                      }
                                      break;

                                  case "day":
                                      this.set_selectedDate(target.date);
                                      this._switchMonth(target.date);
                                      this._blur.post(true);
                                      this.raiseDateSelectionChanged();
                                      break;
                                  case "today":
                                      cal.set_selectedDate(target.date);
                                      cal._switchMonth(target.date);
                                      cal._blur.post(true);
                                      cal.raiseDateSelectionChanged();
                                      break;

                              }

                          })
                      }

                  }

                  function onCalendarShown(sender, args) {
                      //set the default mode to month
                      sender._switchMode("months", true);
                      changeCellHandlers(cal1);
                  }


                  function changeCellHandlers(cal) {

                      if (cal._monthsBody) {

                          //remove the old handler of each month body.
                          for (var i = 0; i < cal._monthsBody.rows.length; i++) {
                              var row = cal._monthsBody.rows[i];
                              for (var j = 0; j < row.cells.length; j++) {
                                  $common.removeHandlers(row.cells[j].firstChild, cal._cell$delegates);
                              }
                          }
                          //add the new handler of each month body.
                          for (var i = 0; i < cal._monthsBody.rows.length; i++) {
                              var row = cal._monthsBody.rows[i];
                              for (var j = 0; j < row.cells.length; j++) {
                                  $addHandlers(row.cells[j].firstChild, cal._cell$delegates);
                              }
                          }
                      }
                  }
                  function onCalendarHidden(sender, args) {
                      //
                      //
                      if (sender.get_selectedDate()) { //
                          if (cal1.get_selectedDate() && cal2.get_selectedDate() && cal1.get_selectedDate() > cal2.get_selectedDate()) {
                              alert('The "From" Date should smaller than the "To" Date, please reselect!');
                              sender.show();
                              return;
                          }
                          //get the final date;
                          var finalDate = new Date(sender.get_selectedDate());
                          var selectedMonth = finalDate.getMonth();
                          finalDate.setDate(1);
                          if (sender == cal2) {
                              // set the calender2's default date as the last day
                              finalDate.setMonth(selectedMonth + 1);
                              finalDate.format(sender._format);
                              finalDate = new Date(finalDate - 1);
                          }
                          //  document.getElementById('').value = finalDate.format("MMM/YYYY");
                          document.getElementById('').value = finalDate.format(sender._format);
                      }
                  }



and adding
VB
OnClientShown="onCalendarShown"                                        OnClientHidden="onCalendarHidden" BehaviorID="calendar1" DefaultView="Months" Format="MMM/yyyy"

in the calender extender
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900