Tuesday, August 4, 2009

How to select a date range of a asp.net calendar control?
Aspx file
Code File
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;

public partial class TestPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { 
            DateTime dt=DateTime.Now;
            Calendar1.SelectedDate = dt;
            Calendar1.VisibleDate = dt;
            lblToDay.Text = "Today is "+dt.Day.ToString()+"/"+dt.Month.ToString()+"/"+dt.Year.ToString();
        }
    }
    protected void Calendar1_PreRender(object sender, EventArgs e)
    {
        
    }
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        DateTime now = DateTime.Now;
        DateTime nextFive = DateTime.Now.AddDays(5);

        if (e.Day.Date <= nextFive && e.Day.Date >= now)
        {
            e.Cell.BackColor = Color.Chocolate;
            e.Cell.Font.Bold = true;   
        }
        else if (e.Day.Date > DateTime.Now.AddDays(10).Date && e.Day.Date <= DateTime.Now.AddDays(12).Date)
        {
            e.Cell.BackColor = Color.Chocolate;
            e.Cell.Font.Bold = true;
        }

        else if (e.Day.Date > DateTime.Now.AddDays(14).Date && e.Day.Date <= DateTime.Now.AddDays(20).Date)
        {
            e.Cell.BackColor = Color.Chocolate;
            e.Cell.Font.Bold = true;
        }
    }
}