In this blog, I explained how to how to highlight a weekend days or a specific days in ASP.NET calender control.
Steps
- Create new ASP.NET Empty Web Application give it to meaning full name.
- Add new webfrom to your project.
- Drag and drop Calendar on your design page.
- Add two properties WeekendDayStyle-BackColor="Yellow", WeekendDayStyle-ForeColor="Black" to your calender control as shown below and run your application.
- <asp:Calendar ID="Calendar1" runat="server"
- WeekendDayStyle-BackColor="Yellow"
- WeekendDayStyle-ForeColor="Green" ></asp:Calendar>
Output
You can achieve the same by using (OnDayRender event) code as mentioned below. Remove WeekendDayStyle-BackColor="Yellow", WeekendDayStyle-ForeColor="Green" from Calender 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.Cell.BackColor = System.Drawing.Color.Yellow;
- e.Cell.ForeColor = System.Drawing.Color.Green;
- }
- }
If you want to Highlight on 'Monday' write the following code in Calendar1_DayRender event. It highlights the the specified day dates.
- if (e.Day.Date.DayOfWeek == DayOfWeek.Monday)
- {
- e.Cell.BackColor = System.Drawing.Color.Yellow;
- e.Cell.ForeColor = System.Drawing.Color.Green;
- }
I hope you enjoyed it. Please provide your valuable suggestions and feedback.