Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

Recently, one of the members at forums.asp.net is asking how to highlight multiple dates in the ASP Calendar and make it selectable and make rest of the un-highlighted dates disabled. So I thought of sharing the solution that I have provided on that thread as a reference to others who might need it.

Here's the code block below:

C#

 

  1. public partial class _Default : System.Web.UI.Page
  2. {
  3.     private List<DateTime> listDates;
  4.     protected void Page_Load(object sender, EventArgs e)
  5.     {
  6.        //Suppose that you have the following list of dates below
  7.         listDates = new List<DateTime>();
  8.         listDates.Add(DateTime.Now);
  9.         listDates.Add(DateTime.Now.AddDays(1));
  10.       
  11.         listDates.Add(DateTime.Now.AddDays(5));
  12.     }
  13.    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
  14.     {
  15.         //Set Default properties
  16.         e.Day.IsSelectable = false;
  17.         e.Cell.BackColor = System.Drawing.Color.Gray;
  18.         //Now loop through the list of dates and make it
  19.         //Selectable
  20.         foreach (DateTime d in listDates) {
  21.             Calendar1.SelectedDates.Add(d);
  22.             if (e.Day.IsSelected) {
  23.                 e.Cell.BackColor = System.Drawing.Color.Green;
  24.                 e.Day.IsSelectable = true;
  25.             }
  26.         }
  27.     }
  28.     protected void Calendar1_SelectionChanged(object sender, EventArgs e)
  29.     {
  30.        //Print the selected date
  31.         Response.Write("You have selected: " + Calendar1.SelectedDate.ToShortDateString());
  32.     }
  33. }

 

VB.NET

 

  1. Public Partial Class _Default
  2.     Inherits System.Web.UI.Page
  3.     Private listDates As List(Of DateTime)
  4.    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
  5.         'Suppose that you have the following list of dates below
  6.         listDates = New List(Of DateTime)()
  7.         listDates.Add(DateTime.Now)
  8.         listDates.Add(DateTime.Now.AddDays(1))
  9.       
  10.         listDates.Add(DateTime.Now.AddDays(5))
  11.     End Sub
  12.     Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs)
  13.        'Set Default properties
  14.         e.Day.IsSelectable = False
  15.         e.Cell.BackColor = System.Drawing.Color.Gray
  16.         'Now loop through the list of dates and make it
  17.         'Selectable
  18.         For Each d As DateTime In listDates
  19.             Calendar1.SelectedDates.Add(d)
  20.             If e.Day.IsSelected Then
  21.                 e.Cell.BackColor = System.Drawing.Color.Green
  22.                 e.Day.IsSelectable = True
  23.             End If
  24.         Next
  25.     End Sub
  26.     Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
  27.         'Print the selected date
  28.         Response.Write("You have selected: " & Calendar1.SelectedDate.ToShortDateString())
  29.    End Sub
  30. End Class

As you can see, the code above is very straight forward and self explanatory. Hope you will find this example useful!

Technorati Tags: ,,
Posted on Monday, February 1, 2010 4:16 PM ASP.NET , C# , Tips&Tricks | Back to top


Comments on this post: Highlight Multiple Dates in Calendar and Make it Selectable

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net