How to disable the Saturdays and Sundays from the Calendar Control in ASP.NET.
The Calendar Control is the part of ASP.NET web controls collection. It is one of the important controls in ASP.NET. The Calendar control belongs to System.Web.UI.WebControls namespace. By natures it provides to the web user to select the date in desired format. We can disable the Saturday and Sunday from the Calendar control in ASP.NET. We have the day render event to disable the Saturday and Sunday from the Calendar control in ASP.NET.
How to disable past days, Saturdays and Sundays in asp.net calendar control
Today we are going to discuss how to disable past days in ASP.NET Calendar control and How to disable the Saturday and Sunday from the Calendar control in ASP.NET. The DayRender event:
The DayRender event is a most useful event in ASP.NET calendar control. This event fires when the calendar is fist rendered on the screen. This will happen as loop for all the dates in the Calendar control every day.
If you want to customize dates in Calendar control you need to write the code in DayRender event according your requirement. Today we are going to see how we can disable or block the past dates and Saturdays and Sundays in your Calendar control. Below is the code to disable the past dates and all weekends from the Calendar control in Asp.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication8.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
protected void DisablePastDays(object sender, DayRenderEventArgs e)
{
if (e.Day.Date < DateTime.Now)
{
e.Cell.Enabled = false;
e.Day.IsSelectable = false;
e.Cell.BackColor = System.Drawing.Color.CadetBlue;
}
if (e.Day.IsWeekend)
{
e.Cell.Enabled = false;
e.Day.IsSelectable = false;
e.Cell.BackColor = System.Drawing.Color.CadetBlue;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Calendar ID="MyCalendar" runat="server" OnDayRender="DisablePastDays"></asp:Calendar>
</div>
</form>
</body>
</html>
From the above code, I added one Calendar control to ASP.NET page and called the event OnDayRender from it. Based on the e.Day.Date property I am disabling the dates. The Output is like as below. We can disable the Saturday and Sundays only from the calendar, below is the code to do that.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication8.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script runat="server">
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date.ToLongDateString().Contains("Sunday") || e.Day.Date.ToLongDateString().Contains("Saturday"))
{
e.Cell.Font.Italic = true;
e.Cell.Font.Size = FontUnit.XLarge;
e.Day.IsSelectable = false;
e.Cell.BackColor = System.Drawing.Color.DarkKhaki;
e.Cell.BorderColor = System.Drawing.Color.Khaki;
e.Cell.ForeColor = System.Drawing.Color.Khaki;
e.Cell.Font.Name = "MS Sans Serif";
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:SlateBlue; font-style:italic;">
How to disable Saturday and Sunday in Calendar control
</h2>
<hr width="475" align="left" color="SlateGray" />
<asp:Calendar
ID="Calendar2"
runat="server"
NextPrevFormat="FullMonth"
ForeColor="Coral"
SelectionMode="Day"
DayNameFormat="Full"
Font-Names="Book Antiqua"
Font-Size="Medium"
OnDayRender="Calendar1_DayRender"
>
<DayHeaderStyle
BackColor="BlueViolet"
/>
<DayStyle
BackColor="SaddleBrown"
BorderColor="Maroon"
BorderWidth="1"
Font-Bold="true"
Font-Italic="true"
Font-Size="Large"
/>
<NextPrevStyle
Font-Italic="true"
Font-Names="Arial CE"
/>
<SelectedDayStyle
BackColor="DarkOliveGreen"
BorderColor="OliveDrab"
/>
<TitleStyle
BackColor="Blue"
Height="36"
Font-Size="Large"
Font-Names="Courier New Baltic"
/>
</asp:Calendar>
</div>
</form>
</body>
</html>
The above code output is like as below.