Friday, 28 September 2012

Beginning .Net : Set Holidays or Note on particular date in asp calendar control with C# Examples and VB.Net Examples

You can set holidays or note on particular date in calendar control. For that we are using "DayRender" Event of the calendar control. This event call on each day cell render, so we can check particular date make set tool tips and also set our user defined backcolor or apply any style in that particular day cell.
You can see tool tip on mouse over of that particular day.

Here is example for this.
In this example we display holidays and notes with two different colors.

ASPX Code :
        <asp:Calendar ID="Calendar1" runat="server"  BackColor="#FFFFCC" 
            BorderColor="#FFCC66" DayNameFormat="Shortest" Font-Names="Verdana" 
            Font-Size="8pt" ForeColor="#663399" Height="200px" Width="220px" 
            BorderWidth="1px" ShowGridLines="True" OnDayRender="Calendar1_DayRender" SelectionMode="None" >
            <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
            <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
            <OtherMonthDayStyle ForeColor="#CC9966" />
            <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
            <SelectorStyle BackColor="#FFCC66" />
            <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" 
                ForeColor="#FFFFCC" />
            <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
        </asp:Calendar>

C# Examples :
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {

        switch(e.Day.Date.ToString("dd/MM/yyyy"))
        {
            case "01/09/2012":
                e.Cell.ToolTip = "Holiday 1";
                e.Cell.BackColor = System.Drawing.Color.Red;
                break;
            case "05/09/2012":
                e.Cell.ToolTip = "Holiday 2";
                e.Cell.BackColor = System.Drawing.Color.Red;
                break;
            case "20/09/2012":
                e.Cell.ToolTip = "Note : Meeting with client";
                e.Cell.BackColor = System.Drawing.Color.Yellow;
                break;

        }

    }

VB.net Examples :
    Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs)

        Select Case e.Day.Date.ToString("dd/MM/yyyy")
            Case "01/09/2012"
                e.Cell.ToolTip = "Holiday 1"
                e.Cell.BackColor = System.Drawing.Color.Red
                Exit Sub
            Case "05/09/2012"
                e.Cell.ToolTip = "Holiday 2"
                e.Cell.BackColor = System.Drawing.Color.Red
                Exit Sub
            Case "20/09/2012"
                e.Cell.ToolTip = "Note : Meeting with client"
                e.Cell.BackColor = System.Drawing.Color.Yellow
                Exit Sub

        End Select

    End Sub

Output :
Set Holidays or Note on particular date in asp calendar control

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.



2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. dot net training and placement in chennai | Dot Net Training in velachery

    ReplyDelete