65.9K
CodeProject is changing. Read more.
Home

customize Calendar Control

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.75/5 (4 votes)

Apr 25, 2007

viewsIcon

24300

downloadIcon

262

customize Calendar Control to add Day Information and day wise formating.

Screenshot - image.jpg

Introduction

Customize calendar control to add day wise information and formating.


Using the code

Control:
 <asp:Calendar ID="Calendar1" runat="server" BackColor="White" 
            BorderColor="Black" Font-Names="Verdana" Font-Size="9pt" 
            ForeColor="Black" Height="250px" NextPrevFormat="ShortMonth" 
            Width="330px" BorderStyle="Solid" CellSpacing="1" 
            OnDayRender="Calendar1_DayRender">
            
            <SelectedDayStyle BackColor="#333399" ForeColor="White" />
            <TodayDayStyle BackColor="#999999" ForeColor="White" />
            <DayStyle BackColor="#CCCCCC" />
            <OtherMonthDayStyle ForeColor="#999999" />
            <NextPrevStyle Font-Size="8pt" ForeColor="White" Font-Bold="True" />
            <DayHeaderStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333"
                Height="8pt" />
            <TitleStyle BackColor="#333399" Font-Bold="True" 
                Font-Size="12pt" ForeColor="White"
                Height="12pt" BorderStyle="Solid" /></asp:Calendar> 


  Code Behind: 

        //array for 12 months and 31 days
    string[,] schedDay = new string[13, 32];

    protected void Page_Load(object sender, EventArgs e)
    {
        // Prepare day(schedule) list before using it.
        schedDay[1, 1] = "1st  Jan";
        schedDay[1, 14] = "KiteDay";
        schedDay[2, 14] = "(?@#$%^&)";
        schedDay[3, 18] = "Don't Know";
        schedDay[4, 1] = "Fool Day";
        schedDay[4, 15] = "MMMMM";
        schedDay[5, 2] = "Don't Know";
        schedDay[6, 8] = "My Day";
        schedDay[7, 21] = "21th  Jul";
        schedDay[8, 11] = "11 - Aug";
        schedDay[9, 18] = "Sep -18";
        schedDay[10, 6] = "LLLFKKKFK";
        schedDay[11, 11] = "11 - 11";
        schedDay[12, 31] = "The - End";
        
    }

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        CalendarDay day = (CalendarDay)e.Day;
        TableCell cell = (TableCell)e.Cell;

        // Check day being rendered is of given month or other.
        // It is required because calander display you some day of previous month 
        // and next moth.
        if (!day.IsOtherMonth)
        {
            String dayStr = schedDay[day.Date.Month, day.Date.Day];
            if (dayStr != null)
            {
                // Format the Cell
                cell.BackColor = System.Drawing.Color.Tan;

                //Write some description about day
                cell.Controls.Add(new LiteralControl("<BR>" + dayStr));
            }
        }
    }
 
By : Paresh [ web.pbp@gmail.com ]

www.pbpworld.co.nr
www.ebooks.110mb.com