CODEDIGEST
Home Articles CodeDigest Tutorials FAQs
Skip Navigation LinksHome » Article » ASP.Net Article » Disable Public holidays in ASP.Net Calendar Control   You are not logged in.
Search
 

Technologies
 

Sponsors
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Disable Public holidays in ASP.Net Calendar Control

By Bala Murugan
Posted On Jul 20,2010
Article Rating: (Login)
Average Rating: 3.67
No of Ratings: 3
No of Comments: 1
Print this article.
Category: ASP.Net

Subscribe to our feed!

Disable Public holidays in ASP.Net Calendar Control

 

The Calendar control is one of the primarily used controls when we build applications that deal with dates. For example, a ticket booking system, attendance registers, etc. There are already some paid and free Calendar controls available in the internet which can be used in these scenarios. ASP.Net control set has also packed with a Calendar control which can satisfy most of our needs when building this type of application. 

 

Read the below article in codedigest which will help us to build our own DatePicker control using the ASP.Net Calendar control.

DatePicker Control in ASP.Net

 

To display a calendar control in your aspx page, you can drag and drop the Calendar control from the visual studio toolbox into to your page. When the page is executed, it will look like,

 

 Calendar Control in ASP.Net

 

Users can select a date by clicking the date displayed in the Calendar control. To get the selected date in codebehind, you can use SelectionChanged event of the Calendar control. Refer below,

 

ASPX

<asp:Calendar ID="calDT" runat="server" onselectionchanged="calDT_SelectionChanged"></asp:Calendar>

CodeBehind

protected void calDT_SelectionChanged(object sender, EventArgs e)

    {

        Response.Write(calDT.SelectedDate.ToString());

    }

 

The Calendar control is flexible enough to be heavily customized in order to fit our requirements.

At times, we may require users not to select some particular dates that are holidays, etc for some business reasons. Moving forward, we will see how this can be achieved when using ASP.Net Calendar control.

 

Calendar control exposes an event called DayRender which can be utilized to control how the dates are displayed in the Calendar control. Hence, our requirement to disable the public holiday or in other words, a set of dates can be done by using this event. The below code does that,

 

ASPX

  <asp:Calendar ID="calDT" runat="server" ondayrender="calDT_DayRender"></asp:Calendar>

   

CodeBehind

List<DateTime> dtholidays = null;

protected void Page_Load(object sender, EventArgs e)

{

dtholidays = GetPublicHolidays();

}

protected void calDT_DayRender(object sender, DayRenderEventArgs e)

    {

        if (dtholidays.Contains(e.Day.Date))

        {

            e.Day.IsSelectable = false;

        }

    }

 

private List<DateTime> GetPublicHolidays()

    {

        List<DateTime> list = new List<DateTime>(); 

        //populate from database or other sources

        list.Add(new DateTime(2010, 01, 01));

        list.Add(new DateTime(2010, 02, 02));

        list.Add(new DateTime(2010, 02, 14));

        list.Add(new DateTime(2010, 03, 02));

        list.Add(new DateTime(2010, 03, 14));

        list.Add(new DateTime(2010, 04, 02));

        list.Add(new DateTime(2010, 04, 14));

        list.Add(new DateTime(2010, 05, 02));

        list.Add(new DateTime(2010, 05, 14));

        list.Add(new DateTime(2010, 06, 02));

        list.Add(new DateTime(2010, 06, 14));

        list.Add(new DateTime(2010, 07, 02));

        list.Add(new DateTime(2010, 07, 14));

        list.Add(new DateTime(2010, 08, 02));

        list.Add(new DateTime(2010, 08, 14));

        list.Add(new DateTime(2010, 09, 02));

        list.Add(new DateTime(2010, 09, 14));

        list.Add(new DateTime(2010, 10, 02));

        list.Add(new DateTime(2010, 10, 14));

        list.Add(new DateTime(2010, 11, 02));

        list.Add(new DateTime(2010, 11, 14));

        list.Add(new DateTime(2010, 12, 02));

        list.Add(new DateTime(2010, 12, 14));

        return list;

    }

 

In the above code, we have disabled the dates that are available in dtholidays list by setting IsSelectable property to false in CalendarDay object. For simplicity, I have just hard coded some dates in dtholidays list, you can build it by fetching it from database or any other data source.

Sometimes, we may also require disabling the weekends (as it is also not a working day) in the calendar control. The CalendarDay object exposed by the DayRenderEventArgs has another Boolean property called IsWeekend which will return true if the day getting rendered is weekend. The above DayRender event can be changed a little to achieve this. Refer below,

 

protected void calDT_DayRender(object sender, DayRenderEventArgs e)

    {

        if (dtholidays.Contains(e.Day.Date) || e.Day.IsWeekend)

        {

            e.Day.IsSelectable = false;

        }

    }

 

The examples discussed above will fully disable the public holidays or to be specific, setting IsSelectable false will remove the link associated with that date which makes the date not selectable in the Calendar control.  But, if your requirement is to allow users to select any dates and just notify the users by highlighting the public holidays then you can provide some styling options to those dates to look like highlighted in DayRender event.

Refer the code below,

protected void calDT_DayRender(object sender, DayRenderEventArgs e)

    {

        if (dtholidays.Contains(e.Day.Date) || e.Day.IsWeekend)

        {           

            e.Cell.BackColor = System.Drawing.Color.Black;

            e.Cell.ForeColor = System.Drawing.Color.White;

        }

    }

 

Once executed, the calendar control will have public holidays and weekend dates background color as black and the font color as white. Refer the below image,

How to highlight or make selected date not selectable in ASP.Net Calendar control?

 

ASP.Net Hosting

Recent Articles

It will also be good and user friendly if we alert the user somehow with a confirm box saying “The selected date is public holiday, do you want to continue?”.

The below code does that,

protected void calDT_DayRender(object sender, DayRenderEventArgs e)

    {

        if (dtholidays.Contains(e.Day.Date) || e.Day.IsWeekend)

        {

            string js = e.SelectUrl;

            string[] jsArr = js.Split(':');

            HyperLink hpl = new HyperLink();

            hpl.Text = ((LiteralControl)e.Cell.Controls[0]).Text;

            hpl.NavigateUrl = "#";

            hpl.Attributes.Add("onclick", jsArr[0] + ":if(!confirm('The selected date is public holiday, do you want to continue?')) { return false; } else { return " + jsArr[1] + ";}");          

            e.Cell.Controls.Clear();

            e.Cell.Controls.Add(hpl);

        }

    }

 

When executed, it will display a confirm box if it is a public holiday. See the below image,

Show javascript confirm box when selecting specific dates or Government holidays in ASP.Net Calendar control

 

Conclusion

The DayRender event exposed by Calendar control allows us to customize the dates displayed in the calendar control heavily.  In this article, we have seen some of the useful customizations that can be done on the Calendar control that makes is more usable.

 

Similar Articles
  • You can contribute to CodeDigest.Com:
    Article Feedback
    Title  
    Submitted By  
    Comment  
    Enter the verification number
     
    Comments
    Article Feedback
    It is very useful to me as i would be able to deselect date which are public holdiday