๐Ÿ“… 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.aspxMarkup file where you add the calendar and other controls
CalendarDemo.aspx.csCode-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)
OnSelectionChangedEvent that fires when user selects a new date
lblSelectedDateShows 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.SelectedDateGets the date the user clicked in the calendar
ToString("format")Converts the DateTime into readable format like “Monday, March 11, 2025”
lblSelectedDate.TextUpdates 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ“… ASP.NET โ€“ Calendars

Or Copy Link

CONTENTS
Scroll to Top