CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

How to disable Weekends(Saturday and Sunday) in ASP.Net Calendar control?
Submitted By Satheesh Babu B
On 7/10/2010 1:11:40 AM
Tags: ASP.Net,CodeDigest  

How to make or prevent Weekends(Saturday and Sunday) in ASP.Net Calendar control?

 

At times, we may have requirements to make the weekends (Saturday and Sunday) as non clickable or selectable in order to prevent the users to select any dates in weekend. For example, if you use calendar control to develop your date picker control and if you want the users to not select any dates in the weekends then it will be good if we can disable the dates in the Calendar control for those days.

 

The below little code snippet will help you to do that by using DayRender event of Calendar control.

 

ASPX
<asp:Calendar ID="Calendar1" runat="server" ondayrender="Calendar1_DayRender">
        </asp:Calendar>

 

CodeBehind
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.IsWeekend)
        {
            e.Day.IsSelectable = false;          
        }
      
    }

 

This IsWeekend property will return true if the rendering day is a weekend. This will make the weekend dates rendered as a text thus making it non selectable.

 

Happy Coding!!

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..