Sunday, June 29, 2008

Calendar Control customization in Visual Studio .NET (C#) code

This is all about Calendar control in .NET technology, visual Studio .Net c# (csharp) code. This can be applicable for both windows and web development.


The following code example demonstrates how to specify and code a handler for the DayRender event to make the background color yellow for the days in the displayed month. It also demonstrates how to customize the contents of a cell by adding a System.Web.UI: LiteralControl control to the cell.


void DayRender(Object source, DayRenderEventArgs e)
{
// Change the background color of the days in the month to yellow.
if (!e.Day.IsOtherMonth && !e.Day.IsWeekend)
e.Cell.BackColor=System.Drawing.Color.Yellow;
// Add custom text to cell in the Calendar control.
if (e.Day.Date.Day == 18)
e.Cell.Controls.Add(new LiteralControl("<br />Holiday"));
}


HTML code block:



<form id="form1" runat="server">
<h3>DayRender Event Example</h3>
<asp:calendar id="calendar1" runat="server" ondayrender="DayRender">
<weekenddaystyle backcolor="gray"></weekenddaystyle>
</asp:calendar>
</form>



C# code behind Calendar event:

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
// Display vacation dates in yellow boxes with purple borders.
Style vacationStyle = new Style();
vacationStyle.BackColor = System.Drawing.Color.Yellow;
vacationStyle.BorderColor = System.Drawing.Color.Purple;
vacationStyle.BorderWidth = 3;

// Display weekend dates in green boxes.
Style weekendStyle = new Style();
weekendStyle.BackColor = System.Drawing.Color.Green;


if ((e.Day.Date >= new DateTime(2000,11,23)) && (e.Day.Date <= new DateTime(2000,11,30)))
{
// Apply the vacation style to the vacation dates.
e.Cell.ApplyStyle(vacationStyle);
}
else if (e.Day.IsWeekend)
{
// Apply the weekend style to the weekend dates.
e.Cell.ApplyStyle(weekendStyle);
}
}

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
DateTime myAppointment = new DateTime(2005, 10, 1);
if (e.Day.Date == myAppointment)
{
e.Day.IsSelectable = true;
}
else
{
e.Day.IsSelectable = false;
}
}


NOTE: This is an example code block which explains about disabling a particular date or a date range. And this code, which is really grabbed a little from a tutorial help forum site, is then modified for our requirement which is what for the blog is.

No comments:

Powered By Blogger