In this article we will introduce with Calendar control in ASP.NET using C#.
Calendar Example in ASP.NET using C#
Calendar control in ASP.NET is used for select the date. The developer can
easily pick the date and use it to in other operation. The user simply drag a
Calendar control from the toolbox and drop it in design page of the application.
You can change the design of the Calendar control by using AutoFormat. When you
click on the AutoFormat a window will open. It has the some auto design of the
Calendar.
The following image shows how we can AutoFormat the Calendar control.
In this example we use the Calendar1_SelectionChanged event of the Calendar control. When user click on date of the Calendar the labels show the current date and selection date.
Calendar.aspx (Design page):
Calendar.aspx (source code):
<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" AutoEventWireup="true" CodeFile="Calendar.aspx.cs" Inherits="Calender" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <div> <h2 style="color:Green">Calendar in ASP.NET 4 , C#</h2> <asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="200px" onselectionchanged="Calendar1_SelectionChanged" ShowGridLines="True" Width="414px"> <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" /> <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" /> <OtherMonthDayStyle ForeColor="#CC9966" /> <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" /> <SelectorStyle BackColor="#FFCC66" /> <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" /> <TodayDayStyle BackColor="#FFCC66" ForeColor="White" /> </asp:Calendar> <br /> <asp:Label ID="label1" runat="server" Font-Bold="True" ForeColor="#000099" /> <br /> <br /> <asp:Label ID="label2" runat="server" Font-Bold="True" ForeColor="#000099" /> </div> </asp:Content>
Calendar.aspx.cs (C# code file):
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Calender : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Calendar1_SelectionChanged(object sender, EventArgs e) { label1.Text = "Current date: " + System.DateTime.Now.ToLongDateString(); label2.Text ="Selected date: " + Calendar1.SelectedDate.ToLongDateString(); } }
Output:
The output is as follows:
Download source code
[ 0 ] Comments