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#
- public partial class _Default : System.Web.UI.Page
- {
- private List<DateTime> listDates;
- protected void Page_Load(object sender, EventArgs e)
- {
-
- listDates = new List<DateTime>();
- listDates.Add(DateTime.Now);
- listDates.Add(DateTime.Now.AddDays(1));
-
- listDates.Add(DateTime.Now.AddDays(5));
- }
- protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
- {
-
- e.Day.IsSelectable = false;
- e.Cell.BackColor = System.Drawing.Color.Gray;
-
-
- foreach (DateTime d in listDates) {
- Calendar1.SelectedDates.Add(d);
- if (e.Day.IsSelected) {
- e.Cell.BackColor = System.Drawing.Color.Green;
- e.Day.IsSelectable = true;
- }
- }
- }
- protected void Calendar1_SelectionChanged(object sender, EventArgs e)
- {
-
- Response.Write("You have selected: " + Calendar1.SelectedDate.ToShortDateString());
- }
- }
VB.NET
- Public Partial Class _Default
- Inherits System.Web.UI.Page
- Private listDates As List(Of DateTime)
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
-
- listDates = New List(Of DateTime)()
- listDates.Add(DateTime.Now)
- listDates.Add(DateTime.Now.AddDays(1))
-
- listDates.Add(DateTime.Now.AddDays(5))
- End Sub
- Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs)
-
- e.Day.IsSelectable = False
- e.Cell.BackColor = System.Drawing.Color.Gray
-
-
- For Each d As DateTime In listDates
- Calendar1.SelectedDates.Add(d)
- If e.Day.IsSelected Then
- e.Cell.BackColor = System.Drawing.Color.Green
- e.Day.IsSelectable = True
- End If
- Next
- End Sub
- Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
-
- Response.Write("You have selected: " & Calendar1.SelectedDate.ToShortDateString())
- End Sub
- 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:
ASP.NET,
C#,
TipsTricks