-->

Tuesday, January 28, 2014

Add Text with day in Calendar, ASP.NET Example

Add Text with day in Calendar, ASP.NET Example

If you want to change the style of the Calendar control, you can do this during its rendering process. DayRender event provides such types of functionality, You can change specific day at run time. Lets take an simple example to add text with day. There are some following steps

Step-1 : Add Calendar control onto the web form
Step-2 : Change Calendar style using show smart tag. In this example, we are selecting Professional1 Style from Auto Format popup.
Step-3 : Handle DayRender Event
Step-4 : Copy this code and replace with your code
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="calendar.aspx.cs" Inherits="calendar" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Calendar ID="Calendar1" runat="server" BackColor="White" 
            BorderColor="White" BorderWidth="1px" Font-Names="Verdana" Font-Size="9pt" 
            ForeColor="Black" Height="295px" NextPrevFormat="FullMonth" 
            ondayrender="Calendar1_DayRender" Width="456px">
            <DayHeaderStyle Font-Bold="True" Font-Size="8pt" />
            <NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333" 
                VerticalAlign="Bottom" />
            <OtherMonthDayStyle ForeColor="#999999" />
            <SelectedDayStyle BackColor="#333399" ForeColor="White" />
            <TitleStyle BackColor="White" BorderColor="Black" BorderWidth="4px" 
                Font-Bold="True" Font-Size="12pt" ForeColor="#333399" />
            <TodayDayStyle BackColor="#CCCCCC" />
        </asp:Calendar>
    </div>
    </form>
</body>
</html>

Code Behind Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class calendar : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day .DayNumberText =="10")
        {
            e.Cell.Controls.Add(new LiteralControl("<p>Holiday</p>"));
            e.Cell.BorderColor = System.Drawing.Color.Green;
            e.Cell.BorderWidth = 2;
            e.Cell.BorderStyle = BorderStyle.Solid;
        }
    }
}

Code Generate the following output

Add Text with day in Calendar, ASP.NET Example

Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved