Customising the Calendar Control in ASP.NET

Initiotech
Posted by in ASP.NET category on for Intermediate level | Views : 13317 red flag

This Article will allow you to customise the Calendar Control in ASP.NET according to your requirements.

Disabling Dates,Formating how dates Appears etc...

This Article will show you how to customise the Calendar Control in ASP.NET.

The Day_Render Event of the Calendar Control is used for many purposes like Enabling and Disabling Valid Dates.
Changing Visiblity of Selected Dates etc.

I will be showing some of the Usages of the Day_Render Event.

A) Restrict users to select date less than todays date.
The Following Code Has to be written in the Day_Render event of the Calendar Control.

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.Date >= DateTime.Today.Date)
        {
            e.Cell.Enabled= false; // Changes the Cells Enable Property
            e.Day.IsSelectable = false; // Disables the Cells Click Property
        }
    }

B) Allow users only to select dates greater than todays date

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.Date <= DateTime.Today.Date)
        {
            e.Cell.Enabled= false; // Changes the Cells Enable Property
            e.Day.IsSelectable = false; // Disables the Cells Click Property
        }
    }

C) Change apprearnce of selected dates and add tooltips or Disable the Click on the Selected Date

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.Date == DateTime.Today.AddDays(-3))//Code for Specific Date
        {
            e.Cell.ForeColor = System.Drawing.Color.Red;
            e.Cell.ToolTip = "Holiday "; // ToolTip for the Date
            e.Day.IsSelectable = false;
        }
        if (e.Day.Date == DateTime.Today.AddDays(-7))//Code for Specific Date
        {
            e.Cell.ForeColor = System.Drawing.Color.Red;
            e.Cell.ToolTip = "Holiday "; // ToolTip for the Date
            e.Day.IsSelectable = false;
        }
    }


D) To Mark weekends on the calander control

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.IsWeekend)
        {
            e.Cell.ForeColor = System.Drawing.Color.Red;
        }
    }

Regards
Hefin Dsouza

Page copy protected against web site content infringement by Copyscape

About the Author

Initiotech
Full Name: Hefin Dsouza
Member Level: Starter
Member Status: Member
Member Since: 9/16/2009 12:32:23 PM
Country: India

http://hefin.in
My Name is Hefin Dsouza. I am a faculty at Computer Education Institute.I am also an Freelance Software Developer and Software Trainner.I am working with DotNet Technologies from the past 8 years.I am an Microsoft Certified Windows Application Developer for .NET 3.5. And also a Blogger at http://blog.hefin.in

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)