๐ ASP.NET โ Calendars โ Beginnerโs Guide to Using the Calendar Control with Code & Output
๐งฒ Introduction โ What Is the ASP.NET Calendar Control?
The ASP.NET Calendar Control is a built-in Web Forms component that lets users view dates, select specific days, and interact with the calendar through server-side C# code. Itโs ideal for scheduling, bookings, event reminders, and forms that require date selection.
ASP.NET handles date formatting, navigation, selection, and styling without writing any JavaScript.
๐ฏ In this guide, youโll learn:
- How to display and interact with the Calendar control
- How to get the selected date in C#
- How to respond to selection and change events
- Full working code with beginner-friendly explanations and output
๐ ASP.NET Web Forms File Structure
๐ File Name | ๐ Description |
---|---|
CalendarDemo.aspx | Markup file where you add the calendar and other controls |
CalendarDemo.aspx.cs | Code-behind file where you handle logic like date selection |
๐ Step 1: Markup File โ CalendarDemo.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CalendarDemo.aspx.cs" Inherits="CalendarDemo.CalendarDemo" %>
<html>
<body>
<form id="form1" runat="server">
<h3>๐
ASP.NET Calendar Control Demo</h3>
<!-- Calendar Control -->
<asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged" />
<br /><br />
<!-- Label to show selected date -->
<asp:Label ID="lblSelectedDate" runat="server" Font-Size="Larger" ForeColor="Blue" />
</form>
</body>
</html>
๐ Explanation of Each Element
Control | ๐ก Purpose |
---|---|
<asp:Calendar ... /> | Displays a calendar (month view by default) |
OnSelectionChanged | Event that fires when user selects a new date |
lblSelectedDate | Shows the selected date using C# backend code |
โ๏ธ Step 2: Code-Behind File โ CalendarDemo.aspx.cs
public partial class CalendarDemo : System.Web.UI.Page
{
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
// Get the selected date
DateTime selected = Calendar1.SelectedDate;
// Show it in a label
lblSelectedDate.Text = "You selected: " + selected.ToString("dddd, MMMM dd, yyyy");
}
}
๐ Beginner Explanation of Code
Code | ๐ฌ What It Does |
---|---|
Calendar1.SelectedDate | Gets the date the user clicked in the calendar |
ToString("format") | Converts the DateTime into readable format like “Monday, March 11, 2025” |
lblSelectedDate.Text | Updates the label to show the chosen date |
๐ฅ๏ธ Output Preview in Browser
โถ๏ธ On Initial Page Load:
๐
[Calendar displayed for current month]
(No message yet)
โถ๏ธ After Selecting March 11, 2025:
๐
[March calendar with 11th selected]
You selected: Tuesday, March 11, 2025
โ The label automatically updates when a new date is selected.
๐ Summary โ Recap & Takeaways
The ASP.NET Calendar control is perfect for date-based forms like bookings, events, and reminders.
๐ Key Learnings:
<asp:Calendar>
displays a calendar and raises events when a date is selected- You use
Calendar1.SelectedDate
to get the selected date in C# - Output can be customized using standard C#
DateTime
formatting - You can dynamically show messages based on selected dates
โ Common uses:
- Hotel bookings
- Event scheduling
- Attendance forms
- Delivery date pickers
โ Frequently Asked Questions (FAQs)
โ Can I set a default selected date in the Calendar?
โ
Yes. In Page_Load
, use:
Calendar1.SelectedDate = DateTime.Today;
โ How can I highlight today’s date or disable past dates?
โ
You can use the DayRender
event for custom logic:
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date < DateTime.Today)
e.Day.IsSelectable = false;
}
โ What format does SelectedDate.ToString()
return?
โ
It returns the full datetime. Use .ToString("d")
for short format or "MMMM dd, yyyy"
for long date.
โ Can I style the calendar with CSS?
โ
Yes! Use CssClass
and style with custom CSS, or set inline styles like BackColor
, Font-Bold
, etc.
Share Now :