Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Asp.net calendar control properties and events - Part 33

Suggested Videos
Part 30 - Fileupload control in asp.net
Part 31 - Adrotator control in asp.net
Part 32 - Asp.net calendar control

Useful Properties of the Calendar control
Caption - This is a string read/write property. 
CaptionAlign - Used to align the caption.
DayHeaderStyle - Style properties that can be used to customize the look and feel of the day header in the calendar
DayNameFormat - Can be Full, Short, FirstLetter, FirstTwoLetters, Shortest
DayStyle - Style properties that can be used to customize the look and feel of the day in the calendar
FirstDayOfWeek - Which day of the week is displayed first
NextPrevFormat - Can be ShortMonth, FullMonth, CustomText
NextMonthText - The text to use for the next month button. 
PrevMonthText - The text to use for the previous month button. 
SelectionMode - Can be Day, DayWeek, DayWeekMonth. Determines if Days, Weeks and Months are selectable.



If the SelectionMode is set to Day, then the user can select only one day. In this case to retrieve the selected date, we use SelectedDate property of the calendar as shown below.
Response.Write(Calendar1.SelectedDate.ToShortDateString());

However, if the SelectionMode is set to DayWeek or DayWeekMonth. In this case of you want to retrieve all the selected dates within the selected week or month, then SelectedDates property can be used as shown below. Using SelectedDate, property returns only the first selected date of the week or month.
foreach (DateTime selectedDate in Calendar1.SelectedDates)
{
    Response.Write(selectedDate.ToShortDateString() + "<br/>");
}

Events:
SelectionChanged - Fires when the date,week or Month selection is changed, by the user.
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
    foreach (DateTime selectedDate in Calendar1.SelectedDates)
    {
        Response.Write(selectedDate.ToShortDateString() + "<br/>");
    }
}



DayRender - Fires as a day in the calendar control is being rendered.
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    if (!e.Day.IsOtherMonth && e.Day.Date.Day % 2 == 0)
    {
        e.Cell.Text = "x";
        e.Cell.Font.Bold = true;
        e.Cell.ForeColor = System.Drawing.Color.White;
        e.Cell.BackColor = System.Drawing.Color.Red;
        e.Cell.ToolTip = "Booked";
    }
    else
    {
        e.Cell.ToolTip = "Available";
    }
}

VisibleMonthChanged - Fires when the visible month is changed by the user
protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
{
    Response.Write("Month changed from ");
    Response.Write(GetMonthName(e.PreviousDate.Month));
    Response.Write(" to ");
    Response.Write(GetMonthName(e.NewDate.Month));
}
private string GetMonthName(int MonthNumber)
{
    switch (MonthNumber)
    {
    case 1:
        return "Jan";
    case 2:
        return "Feb";
    case 3:
        return "Mar";
    case 4:
        return "Apr";
    case 5:
        return "May";
    case 6:
        return "Jun";
    case 7:
        return "Jul";
    case 8:
        return "Aug";
    case 9:
        return "Sep";
    case 10:
        return "Oct";
    case 11:
        return "Nov";
    case 12:
        return "Dec";
    default:
        return "Invalid Month";
    }
}

8 comments:

  1. sir,will you please tell me how to insert DATE obtained from a calender in asp to the database.

    ReplyDelete
  2. Hi, When I click the image button - the calendar appears, selection happens properly as well. But when the Calendar appears, other controls placed below it moves down (and after date selection, the other controls move up again).

    I know this is simple, but am stuck here. Please help.

    TIA.

    ReplyDelete
    Replies
    1. Hi,
      I have the create the same application but nothing out of the ordinary happens. the only change is that when response.write executes, the page aligns to write the response on top. I cant seem to replicate the issue that you are facing

      Delete
  3. hi Sir
    please tell about the .mdb database connections in asp.net

    ReplyDelete
  4. sir, can i get an answer for controlling calendar to select next 4 days from the current date and block rest dates

    ReplyDelete
    Replies
    1. protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
      {
      if (!e.Day.IsOtherMonth && (e.Day.Date.Equals(DateTime.Now.Date) || e.Day.Date.AddDays(-1).Equals(DateTime.Now.Date) ||
      e.Day.Date.AddDays(-2).Equals(DateTime.Now.Date) || e.Day.Date.AddDays(-3).Equals(DateTime.Now.Date) || e.Day.Date.AddDays(-4).Equals(DateTime.Now.Date)))
      {
      e.Day.IsSelectable = true;
      }
      else {
      e.Day.IsSelectable = false;
      }
      }

      Delete
    2. protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
      {
      if (!e.Day.IsOtherMonth && !e.Day.IsWeekend &&
      e.Day.Date >= DateTime.Today.AddDays(-10) && e.Day.Date <= DateTime.Today.AddDays(+9))
      {
      e.Day.IsSelectable = true;
      }
      else
      {
      e.Day.IsSelectable = false;
      e.Cell.BackColor = System.Drawing.Color.LightGray;
      }
      }

      Delete
  5. hello sir, how can i get an controlling calendar to select next 5 workingdays from Sunday to Thursday??

    ReplyDelete

It would be great if you can help share these free resources