How To Disable Weekends(Saturday and Sunday) In Asp.Net Calender Control

How To Disable Weekends(Saturday and Sunday) In Asp.Net Calender Control

In this post we will learn How To Disable Weekends(Saturday and Sunday) In Asp.Net Calender Control. Some 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.

Design View:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How To Disable Weekends(Saturday and Sunday) In Asp.Net Calender Control</title>
<style type="text/css">
body
 {
 width: 980px;
 margin: 0px auto;
 text-align: center;
 padding-top: 50px;
 font-size: 18px;
 font-family:Calibri;
 }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>How To Disable Weekends(Saturday and Sunday) In Asp.Net Calender Control</h2>
<br /><br />
<div style="width:350px;margin:0px auto"> 
<asp:Calendar ID="Calendar1" runat="server" OnDayRender="Calendar1_DayRender"></asp:Calendar>
</div>
<br /><br />
<br /><br />
All rights reserved by <a href="http://www.hightechnology.in">www.Hightechnology.in</a>
| Hosting partner <a href="http://www.grootstech.com" target="_blank">Grootstech</a>   
</div>
</form>
</body>
</html>

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 _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

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

demo