ASP.Net/Asp Control/Calendar
Содержание
- 1 Add control to Calendar cell
- 2 Allowing the Selection of Multiple Dates in a Calendar Control (VB.net)
- 3 Assign today"s date to asp calendar (VB.net)
- 4 Basic Calendar Control: show grid lines, show day header (VB.net)
- 5 Calendar Control: day name format, first day of week (VB.net)
- 6 Calendar control in code behind (C#)
- 7 Calendar Control: nextprevformat (VB.net)
- 8 Calendar control: on day render (VB.net)
- 9 Calendar Control: titleformat (VB.net)
- 10 Calendar events: render and change events
- 11 Calendar selected value changed event (VB.net)
- 12 Calendar selection changed event
- 13 Calendar selection mode: Day
- 14 Calendar Control
- 15 Calendar Control
- 16 Calendar Control
- 17 Find a Flight
- 17.1 Controlling the Appearance of Individual Cells of Dates (VB.net)
- 17.2 Convert selected date from asp:Calendar to long string (VB.net)
- 17.3 Date Selection event in a Calendar Control (VB.net)
- 17.4 Day renderer
- 17.5 Define and use calendar in code behind (C#)
- 17.6 Displaying a Date in the Calendar Control (VB.net)
- 17.7 Displaying a Selected Range of Dates in the Calendar Control (VB.net)
- 17.8 Embed Javascript to C# code to create a popup window
- 17.9 Formatting Date Sections in a Calendar Control (VB.net)
- 17.10 Formatting the Header Styles in a Calendar Control (VB.net)
- 17.11 Get selected date from asp:Calendar (VB.net)
- 17.12 Load event in calendar selection event (C#)
- 17.13 On Date and Month Selection Changed (VB.net)
- 17.14 On visible month changed event for a calendar control (VB.net)
- 17.15 Popup calendar
- 17.16 Reading Selected Dates in a Date Range through the Calendar Control (VB.net)
- 17.17 Select a week (VB.net)
- 17.18 Selected value change envent for asp:Calendar (VB.net)
- 17.19 Set asp calendar border (VB.net)
- 17.20 Set DayStyle, NextPrevStyle, DayHeaderStyle, TitleStyle, WeekendDayStyle
- 17.21 Setting Custom Previous and Next Month Text in the Calendar Control (VB.net)
- 17.22 Set TodayDayStyle: background color and foreground color
- 17.23 Visible month changed event
Add control to Calendar cell
<source lang="csharp">
<%@ Page Language="C#" %> <HTML>
<body> <script runat="server"> void PrettyCalendar_OnDayRender (object Sender, System.Web.UI.WebControls.DayRenderEventArgs e) { if (e.Day.Date == System.DateTime.Today) { e.Cell.Controls.Add(new LiteralControl("
")); e.Cell.Controls.Add(new LiteralControl("Today!")); } } </script> <form runat="server" ID="Form1"> <asp:Calendar ID="PrettyCalendar" Runat="server" OnDayRender="PrettyCalendar_OnDayRender"> <TodayDayStyle ForeColor="Red" BackColor="Aqua"></TodayDayStyle> <DayStyle Font-Bold="True" HorizontalAlign="Left" Height="90px" BorderWidth="1px" BorderStyle="Solid" BorderColor="Red" Width="100px" VerticalAlign="Top" BackColor="Yellow"></DayStyle> <NextPrevStyle ForeColor="Blue"/> <DayHeaderStyle Font-Size="Large" Font-Bold="True" BorderWidth="1px" ForeColor="Brown" BorderStyle="Solid" BorderColor="Black" Width="100px" BackColor="#00C0C0"></DayHeaderStyle> <TitleStyle Font-Size="Large" Font-Bold="True" BorderWidth="1px" BorderStyle="Solid" BorderColor="Black" BackColor="#80FFFF"></TitleStyle> <WeekendDayStyle BackColor="#C0C0C0"></WeekendDayStyle> </asp:Calendar> </form> </body>
</HTML>
</source>
Allowing the Selection of Multiple Dates in a Calendar Control (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Allowing the Selection of Multiple Dates in a Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<asp:calendar
id="Mycal" runat="server" cellpadding="3" cellspacing="3" selectionmode="DayWeekMonth" selectorstyle-font-bold="True" selectorStyle-backcolor="#3366ff" font-name="Tahoma" font-size="12" backcolor="ivory" selecteddaystyle-backcolor="#ffcc66" selecteddaystyle-font-bold="True"
/> </form> </BODY> </HTML>
</source>
Assign today"s date to asp calendar (VB.net)
<source lang="csharp">
<%@ Page Language="VB" Trace="true" %> <script runat="server">
Sub Page_Load() Label1.Text = "www.nfex.ru" Calendar1.TodaysDate = DateTime.Now End Sub
</script> <html> <head>
<title>Show Trace</title>
</head> <body>
<form id="form1" runat="server">
<asp:Label id="Label1" Runat="server" /> <asp:Calendar id="Calendar1" TodayDayStyle-BackColor="Yellow" Runat="server" />
</form>
</body> </html>
</source>
Basic Calendar Control: show grid lines, show day header (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Creating a Basic Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<asp:Calendar
id="Mycal" runat="server" cellpadding="3" cellspacing="3" showgridlines="true" showdayheader="False" shownextprevmonth="False" showtitle="false"
/> </form> </BODY> </HTML>
</source>
Calendar Control: day name format, first day of week (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Creating a Basic Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<asp:Calendar
id="Mycal" runat="server" cellpadding="3" cellspacing="3" daynameformat="Short" firstdayofweek="Monday" nextprevformat="FullMonth" titleformat="MonthYear" font-name="Tahoma" font-size="12" backcolor="ivory"
/> </form> </BODY> </HTML>
</source>
Calendar control in code behind (C#)
<source lang="csharp">
<%@ Page language="c#" src="CalendarTest.aspx.cs" AutoEventWireup="false" Inherits="CalendarTest" %> <HTML>
<body> <form id="Form1" method="post" runat="server">
<asp:Calendar id="Calendar1" runat="server" SelectionMode="DayWeekMonth" BorderWidth="1px" BackColor="#FFFFCC" Width="220px" DayNameFormat="FirstLetter" ForeColor="#663399" Height="200px" Font-Size="8pt" Font-Names="Verdana" BorderColor="#FFCC66" ShowGridLines="True"> <TodayDayStyle ForeColor="White" BackColor="#FFCC66"></TodayDayStyle> <SelectorStyle BackColor="#FFCC66"></SelectorStyle> <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC"></NextPrevStyle> <DayHeaderStyle Height="1px" BackColor="#FFCC66"></DayHeaderStyle> <SelectedDayStyle Font-Bold="True" BackColor="#CCCCFF"></SelectedDayStyle> <TitleStyle Font-Size="9pt" Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></TitleStyle> <OtherMonthDayStyle ForeColor="#CC9966"></OtherMonthDayStyle> </asp:Calendar>
<asp:Label id="lbl" runat="server" Width="544px" Height="72px" Font-Size="Small" Font-Names="Verdana" Font-Bold="True">Look for the month of May ...</asp:Label>
</form> </body>
</HTML>
<%-- CalendarTest.aspx.cs using System; using System.Collections; using System.ruponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
public class CalendarTest : System.Web.UI.Page { protected System.Web.UI.WebControls.Calendar Calendar1; protected System.Web.UI.WebControls.Label lbl; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Calendar1.DayRender += new System.Web.UI.WebControls.DayRenderEventHandler(this.Calendar1_DayRender); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Calendar1_DayRender(Object source, DayRenderEventArgs e) { // Check for May 5 in any year, and format it. if (e.Day.Date.Day == 5 && e.Day.Date.Month == 5) { e.Cell.BackColor = System.Drawing.Color.Yellow; // Add some static text to the cell. Label lbl = new Label(); lbl.Text = "
My Birthday!"; e.Cell.Controls.Add(lbl); } } }
--%>
</source>
Calendar Control: nextprevformat (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Creating a Basic Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<asp:Calendar
id="Mycal" runat="server" cellpadding="3" cellspacing="3" daynameformat="Short" firstdayofweek="Monday" nextprevformat="FullMonth" titleformat="MonthYear" font-name="Tahoma" font-size="12" backcolor="ivory"
/> </form> </BODY> </HTML>
</source>
Calendar control: on day render (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %> <script runat=server> Sub DateDisplayed(source As Object, e As DayRenderEventArgs)
If e.Day.Date.Month = "2" and e.Day.Date.Day = "14" Then e.Cell.BackColor = System.Drawing.Color.Red e.Cell.ForeColor = System.Drawing.Color.Yellow ElseIf e.Day.Date.Month = "12" and e.Day.Date.Day = "25" Then e.Cell.BackColor = System.Drawing.Color.Green e.Cell.ForeColor = System.Drawing.Color.Red ElseIf e.Day.Date.Month = "10" and e.Day.Date.Day = "31" Then e.Cell.BackColor = System.Drawing.Color.Orange e.Cell.ForeColor = System.Drawing.Color.Black End If
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Controlling the Appearance of Individual Cells of Dates in the Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<asp:Calendar
id="Mycal" runat="server" cellpadding="3" cellspacing="3" ondayrender="DateDisplayed"
/> </form> </BODY> </HTML>
</source>
Calendar Control: titleformat (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Creating a Basic Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<asp:Calendar
id="Mycal" runat="server" cellpadding="3" cellspacing="3" daynameformat="Short" firstdayofweek="Monday" nextprevformat="FullMonth" titleformat="MonthYear" font-name="Tahoma" font-size="12" backcolor="ivory"
/> </form> </BODY> </HTML>
</source>
Calendar events: render and change events
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Calendar</title>
</head> <body>
<form id="form1" runat="server">
<asp:Label id="lblMonthChanged" runat="server" /> <asp:Calendar ID="Calendar1" runat="server" SelectionMode="DayWeekMonth" CellPadding="7" CellSpacing="5" DayNameFormat="FirstTwoLetters" FirstDayOfWeek="Monday" NextMonthText="Next >" PrevMonthText="< Prev" ShowGridLines="True" DayStyle-BackColor="White" DayStyle-ForeColor="Black" DayStyle-Font-Names="Arial" OnSelectionChanged="Calendar1_SelectionChanged" OnDayRender="Calendar1_DayRender" OnVisibleMonthChanged="Calendar1_VisibleMonthChanged"> <DayHeaderStyle BackColor="Black" Font-Names="Arial Black" ForeColor="White" /> <SelectedDayStyle BackColor="Cornsilk" Font-Bold="True" Font-Italic="True" Font-Names="Arial" ForeColor="Blue" /> <SelectorStyle BackColor="Cornsilk" Font-Names="Arial" ForeColor="Red" /> <WeekendDayStyle BackColor="LavenderBlush" Font-Names="Arial" ForeColor="Purple" /> <OtherMonthDayStyle BackColor="LightGray" Font-Names="Arial" ForeColor="White" /> <TodayDayStyle BackColor="Cornsilk" Font-Bold="True" Font-Names="Arial" ForeColor="Green" /> <NextPrevStyle BackColor="DarkGray" Font-Names="Arial" ForeColor="Yellow" /> <TitleStyle BackColor="Gray" Font-Names="Arial Black" ForeColor="White" HorizontalAlign="Left" /> </asp:Calendar>
<asp:Label id="lblCount" runat="server" />
<asp:Label id="lblTodaysDate" runat="server" />
<asp:Label id="lblSelected" runat="server" />
Select a month: |
<asp:DropDownList id= "ddl" AutoPostBack="true" onSelectedIndexChanged = "ddl_SelectedIndexChanged" runat="server"> <asp:ListItem text="January" value="1" /> <asp:ListItem text="February" value="2" /> <asp:ListItem text="March" value="3" /> <asp:ListItem text="May" value="5" /> <asp:ListItem text="June" value="6" /> <asp:ListItem text="July" value="7" /> <asp:ListItem text="August" value="8" /> <asp:ListItem text="September" value="9" /> <asp:ListItem text="October" value="10" /> <asp:ListItem text="November" value="11" /> <asp:ListItem text="December" value="12" /> </asp:DropDownList> |
<asp:Button id="btnTgif" text="TGIF" onClick="btnTgif_Click" runat="server" /> |
Day Range | ||
Starting Day | Ending Day | |
<asp:TextBox id= "txtStart" runat="server" Width="25" MaxLength="2" /> |
<asp:TextBox id= "txtEnd" runat="server" Width="25" MaxLength="2" /> |
<asp:Button id="btnRange" runat="server" text="Apply" onClick="btnRange_Click" /> |
</form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Calendar1.VisibleDate = Calendar1.TodaysDate; ddl.SelectedIndex = Calendar1.VisibleDate.Month - 1; } lblTodaysDate.Text = "Today"s Date is " + Calendar1.TodaysDate.ToShortDateString(); } protected void Calendar1_SelectionChanged(object sender, EventArgs e) { lblSelectedUpdate(); lblCountUpdate(); txtClear(); } private void lblSelectedUpdate() { if (Calendar1.SelectedDate != DateTime.MinValue) lblSelected.Text = "The date selected is " + Calendar1.SelectedDate.ToShortDateString(); } private void lblCountUpdate() { lblCount.Text = "Count of Days Selected: " + Calendar1.SelectedDates.Count.ToString(); } protected void ddl_SelectedIndexChanged(Object sender, EventArgs e) { Calendar1.SelectedDates.Clear(); lblSelectedUpdate(); lblCountUpdate(); Calendar1.VisibleDate = new DateTime(Calendar1.VisibleDate.Year, Int32.Parse(ddl.SelectedItem.Value), 1); txtClear(); } protected void btnTgif_Click(Object sender, EventArgs e) { int currentMonth = Calendar1.VisibleDate.Month; int currentYear = Calendar1.VisibleDate.Year; Calendar1.SelectedDates.Clear(); for (int i = 1; i <= System.DateTime.DaysInMonth(currentYear,currentMonth); i++) { DateTime date = new DateTime(currentYear, currentMonth, i); if (date.DayOfWeek == DayOfWeek.Friday) Calendar1.SelectedDates.Add(date); } lblSelectedUpdate(); lblCountUpdate(); txtClear(); } protected void btnRange_Click(Object sender, EventArgs e) { int currentMonth = Calendar1.VisibleDate.Month; int currentYear = Calendar1.VisibleDate.Year; DateTime StartDate = new DateTime(currentYear, currentMonth, Int32.Parse(txtStart.Text)); DateTime EndDate = new DateTime(currentYear, currentMonth, Int32.Parse(txtEnd.Text)); Calendar1.SelectedDates.Clear(); Calendar1.SelectedDates.SelectRange(StartDate, EndDate); lblSelectedUpdate(); lblCountUpdate(); } private void txtClear() { txtStart.Text = ""; txtEnd.Text = ""; } protected void Calendar1_DayRender(object sender, DayRenderEventArgs e) { if (!e.Day.IsOtherMonth && e.Day.IsWeekend) e.Cell.BackColor = System.Drawing.Color.LightGreen; if (e.Day.Date.Month == 1 && e.Day.Date.Day == 1) e.Cell.Controls.Add(new LiteralControl("
Happy New Year!")); } protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e) { if ((e.NewDate.Year > e.PreviousDate.Year) || ((e.NewDate.Year == e.PreviousDate.Year) && (e.NewDate.Month > e.PreviousDate.Month))) lblMonthChanged.Text = "My future"s so bright..."; else lblMonthChanged.Text = "Back to the future!"; Calendar1.SelectedDates.Clear(); lblSelectedUpdate(); lblCountUpdate(); txtClear(); }
}
</source>
Calendar selected value changed event (VB.net)
<source lang="csharp">
<%@ Import namespace="System.Data" %> <%@ Import namespace="System.Data.SqlClient" %> <html>
<head><title>Using the Calendar</title></head> <body>
Using the Calendar
<form runat="server"> <asp:Label id="lblSelectedDate" runat="server" /> <asp:Calendar id="calHire" runat="Server" SelectionMode = "DayWeek" OnSelectionChanged = "CalendarChange" /> </form> </body>
</html> <script language="VB" runat="server"> Sub CalendarChange(Source As Object, E As EventArgs)
lblSelectedDate.Text = calHire.SelectedDate
End Sub </script>
</source>
Calendar selection changed event
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Calendar</title>
</head> <body>
<form id="form1" runat="server">
<asp:Calendar ID="Calendar1" runat="server" SelectionMode="DayWeekMonth" CellPadding="7" CellSpacing="5" DayNameFormat="FirstTwoLetters" FirstDayOfWeek="Monday" NextMonthText="Next >" PrevMonthText="< Prev" ShowGridLines="True" DayStyle-BackColor="White" DayStyle-ForeColor="Black" DayStyle-Font-Names="Arial" OnSelectionChanged="Calendar1_SelectionChanged"> <DayHeaderStyle BackColor="Black" Font-Names="Arial Black" ForeColor="White" /> <SelectedDayStyle BackColor="Cornsilk" Font-Bold="True" Font-Italic="True" Font-Names="Arial" ForeColor="Blue" /> <SelectorStyle BackColor="Cornsilk" Font-Names="Arial" ForeColor="Red" /> <WeekendDayStyle BackColor="LavenderBlush" Font-Names="Arial" ForeColor="Purple" /> <OtherMonthDayStyle BackColor="LightGray" Font-Names="Arial" ForeColor="White" /> <TodayDayStyle BackColor="Cornsilk" Font-Bold="True" Font-Names="Arial" ForeColor="Green" /> <NextPrevStyle BackColor="DarkGray" Font-Names="Arial" ForeColor="Yellow" /> <TitleStyle BackColor="Gray" Font-Names="Arial Black" ForeColor="White" HorizontalAlign="Left" /> </asp:Calendar>
<asp:Label id="lblCount" runat="server" />
<asp:Label id="lblTodaysDate" runat="server" />
<asp:Label id="lblSelected" runat="server" />
</form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Default : System.Web.UI.Page {
protected void Calendar1_SelectionChanged(object sender, EventArgs e) { lblTodaysDate.Text = "Today"s Date is " + Calendar1.TodaysDate.ToShortDateString(); if (Calendar1.SelectedDate != DateTime.MinValue) lblSelected.Text = "The date selected is " + Calendar1.SelectedDate.ToShortDateString(); lblCountUpdate(); } private void lblCountUpdate() { lblCount.Text = "Count of Days Selected: " + Calendar1.SelectedDates.Count.ToString(); }
}
</source>
Calendar selection mode: Day
<source lang="csharp">
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Calendar</title>
</head> <body>
<form id="form1" runat="server">
Calendar Control
SelectionMode
<asp:Calendar ID="Calendar1" runat="server" SelectionMode="Day"></asp:Calendar>
</form>
</body> </html>
</source>
Calendar selection mode: DayWeek
<source lang="csharp">
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Calendar</title>
</head> <body>
<form id="form1" runat="server">
Calendar Control
SelectionMode
<asp:Calendar ID="Calendar1" runat="server" SelectionMode="DayWeek"></asp:Calendar>
</form>
</body> </html>
</source>
Calendar selection mode: DayWeekMonth
<source lang="csharp">
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Calendar</title>
</head> <body>
<form id="form1" runat="server">
Calendar Control
SelectionMode
<asp:Calendar ID="Calendar1" runat="server" SelectionMode="DayWeekMonth"></asp:Calendar>
</form>
</body> </html>
</source>
Calendar with TodayDayStyle, SelectorStyle, NextPrevStyle, and SelectedDayStyle (C#)
<source lang="csharp">
<%@ Page language="c#" src="CalendarTest.aspx.cs" AutoEventWireup="false" Inherits="CalendarTest" %> <HTML>
<body> <form id="Form1" method="post" runat="server">
<asp:Calendar id="Calendar1" runat="server" SelectionMode="DayWeekMonth" BorderWidth="1px" BackColor="#FFFFCC" Width="220px" DayNameFormat="FirstLetter" ForeColor="#663399" Height="200px" Font-Size="8pt" Font-Names="Verdana" BorderColor="#FFCC66" ShowGridLines="True"> <TodayDayStyle ForeColor="White" BackColor="#FFCC66"></TodayDayStyle> <SelectorStyle BackColor="#FFCC66"></SelectorStyle> <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC"></NextPrevStyle> <DayHeaderStyle Height="1px" BackColor="#FFCC66"></DayHeaderStyle> <SelectedDayStyle Font-Bold="True" BackColor="#CCCCFF"></SelectedDayStyle> <TitleStyle Font-Size="9pt" Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></TitleStyle> <OtherMonthDayStyle ForeColor="#CC9966"></OtherMonthDayStyle> </asp:Calendar>
<asp:Label id="lbl" runat="server" Width="544px" Height="72px" Font-Size="Small" Font-Names="Verdana" Font-Bold="True">Look for the month of May ...</asp:Label>
</form> </body>
</HTML>
<%-- CalendarTest.aspx.cs using System; using System.Collections; using System.ruponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
public class CalendarTest : System.Web.UI.Page { protected System.Web.UI.WebControls.Calendar Calendar1; protected System.Web.UI.WebControls.Label lbl; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Calendar1.DayRender += new System.Web.UI.WebControls.DayRenderEventHandler(this.Calendar1_DayRender); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Calendar1_DayRender(Object source, DayRenderEventArgs e) { // Check for May 5 in any year, and format it. if (e.Day.Date.Day == 5 && e.Day.Date.Month == 5) { e.Cell.BackColor = System.Drawing.Color.Yellow; // Add some static text to the cell. Label lbl = new Label(); lbl.Text = "
My Birthday!"; e.Cell.Controls.Add(lbl); } } }
--%>
</source>
Change Calendar selection based on data from the database (VB.net)
<source lang="csharp">
<%@ Import namespace="System.Data" %> <%@ Import namespace="System.Data.SqlClient" %> <html>
<head><title>Calendar: Hire Dates</title></head> <body>
Calendar: Hire Dates
<form runat="server"> <asp:ListBox id="lstEmployees" runat="server" Rows="7" Autopostback="True" />
<asp:Label id="lblSelectedDate" runat="server" /> <asp:Calendar id="calHire" runat="server" SelectionMode = "None" /> </form> </body>
</html> <script language="VB" runat="server"> Sub Page_Load(Source As Object, E As EventArgs)
Dim strConnection As String = ConfigurationSettings.AppSettings("YourDataName") Dim objConnection As New SqlConnection(strConnection) Dim strSQL As String = "SELECT ID, LastName, FirstName, HireDate FROM Employee;" Dim objAdapter As New SqlDataAdapter(strSQL, objConnection) Dim objDataSet As New DataSet("dsEmployees") objAdapter.Fill(objDataSet, "dtEmployees") If Not IsPostBack Then lstEmployees.DataSource = objDataSet lstEmployees.DataTextField = "LastName" lstEmployees.DataBind() Else Dim datHireDate As Date datHireDate = Convert.ToDateTime( _
objDataSet.Tables("dtEmployees").Rows(lstEmployees.SelectedIndex)("HireDate"))
lblSelectedDate.Text = _
objDataSet.Tables("dtEmployees").Rows(lstEmployees.SelectedIndex)("LastName")
calHire.VisibleDate = datHireDate calHire.SelectedDate = datHireDate lblSelectedDate.Text &= " hired on " & datHireDate End If
End Sub </script>
</source>
Change size of Calendar
<source lang="csharp">
<%@ Page Language="C#" %> <HTML>
<body> <script runat="server"> void PrettyCalendar_OnDayRender (object Sender, System.Web.UI.WebControls.DayRenderEventArgs e) { if (e.Day.Date == System.DateTime.Today) { e.Cell.Controls.Add(new LiteralControl("
")); e.Cell.Controls.Add(new LiteralControl("Today!")); } } </script> <form runat="server" ID="Form1"> <asp:Calendar ID="PrettyCalendar" Runat="server" OnDayRender="PrettyCalendar_OnDayRender"> <TodayDayStyle ForeColor="Red" BackColor="Aqua"></TodayDayStyle> <DayStyle Font-Bold="True" HorizontalAlign="Left" Height="90px" BorderWidth="1px" BorderStyle="Solid" BorderColor="Red" Width="100px" VerticalAlign="Top" BackColor="Yellow"></DayStyle> <NextPrevStyle ForeColor="Blue"/> <DayHeaderStyle Font-Size="Large" Font-Bold="True" BorderWidth="1px" ForeColor="Brown" BorderStyle="Solid" BorderColor="Black" Width="100px" BackColor="#00C0C0"></DayHeaderStyle> <TitleStyle Font-Size="Large" Font-Bold="True" BorderWidth="1px" BorderStyle="Solid" BorderColor="Black" BackColor="#80FFFF"></TitleStyle> <WeekendDayStyle BackColor="#C0C0C0"></WeekendDayStyle> </asp:Calendar> </form> </body>
</HTML>
</source>
Check the range of Calendar controls
<source lang="csharp">
<%@ Page Language="C#" %> <HTML>
<body> <script runat="server"> void Page_Load(object sender, EventArgs e) { if(!IsPostBack) DepartureDate.SelectedDate = System.DateTime.Today; } void SearchButton_Click(object Sender, EventArgs e) { if(DepartureDate.SelectedDate <= System.DateTime.Today) ResultLabel.Text = "You must selected a Departure Date in the future."; else if(ReturnDate.SelectedDate < DepartureDate.SelectedDate) ResultLabel.Text = "Return Date must follow the Departure Date."; else ResultLabel.Text = "Departing on " + DepartureDate.SelectedDate.ToShortDateString() + " and returning on " + ReturnDate.SelectedDate.ToShortDateString(); } </script> <form runat="server" ID="Form1">
Find a Flight
Departure Date | <asp:Calendar ID="DepartureDate" Runat="server">
<DayStyle Font-Size="X-Small"></DayStyle> <TitleStyle Font-Bold="True"></TitleStyle></asp:Calendar> |
Return Date | <asp:Calendar ID="ReturnDate" Runat="server">
<DayStyle Font-Size="X-Small"></DayStyle> <TitleStyle Font-Bold="True"></TitleStyle></asp:Calendar> |
<asp:Button ID="SearchButton" Runat="server" Text="Search" OnClick="SearchButton_Click" />
<asp:Label ID="ResultLabel" Runat="server" /> </form> </body>
</HTML>
</source>
Controlling the Appearance of Individual Cells of Dates (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %> <script runat=server> Sub DateDisplayed(source As Object, e As DayRenderEventArgs)
If e.Day.Date.Month = "2" and e.Day.Date.Day = "14" Then e.Cell.BackColor = System.Drawing.Color.Red e.Cell.ForeColor = System.Drawing.Color.Yellow ElseIf e.Day.Date.Month = "12" and e.Day.Date.Day = "25" Then e.Cell.BackColor = System.Drawing.Color.Green e.Cell.ForeColor = System.Drawing.Color.Red ElseIf e.Day.Date.Month = "10" and e.Day.Date.Day = "31" Then e.Cell.BackColor = System.Drawing.Color.Orange e.Cell.ForeColor = System.Drawing.Color.Black End If
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Controlling the Appearance of Individual Cells of Dates in the Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<asp:Calendar
id="Mycal" runat="server" cellpadding="3" cellspacing="3" ondayrender="DateDisplayed"
/> </form> </BODY> </HTML>
</source>
Convert selected date from asp:Calendar to long string (VB.net)
<source lang="csharp">
<%@ Page Language="VB" %> <script runat="server">
Sub Button1_Click(sender As Object, e As EventArgs) Label1.Text = Calendar1.SelectedDate.ToLongDateString() End Sub
</script> <html> <head> </head> <body>
<form runat="server">
<asp:Calendar id="Calendar1" runat="server"></asp:Calendar>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
<asp:Label id="Label1" runat="server">Label</asp:Label>
</form>
</body> </html>
</source>
Date Selection event in a Calendar Control (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %> <script runat=server> Sub calSelectChange(ByVal Sender as Object, ByVal E as EventArgs)
lblMessage.Text = "You selected " _ & MyCal.SelectedDate
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Writing Code that Fires when a Date is Selected in a Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<asp:Calendar
id="Mycal" runat="server" cellpadding="3" cellspacing="3" onselectionchanged="calSelectChange"
/>
<asp:Label
id="lblMessage" runat="server" Font-Bold="True"
/> </form> </BODY> </HTML>
</source>
Day renderer
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Calendar</title>
</head> <body>
<form id="form1" runat="server">
<asp:Label id="lblMonthChanged" runat="server" /> <asp:Calendar ID="Calendar1" runat="server" SelectionMode="DayWeekMonth" CellPadding="7" CellSpacing="5" DayNameFormat="FirstTwoLetters" FirstDayOfWeek="Monday" NextMonthText="Next >" PrevMonthText="< Prev" ShowGridLines="True" DayStyle-BackColor="White" DayStyle-ForeColor="Black" DayStyle-Font-Names="Arial" OnSelectionChanged="Calendar1_SelectionChanged" OnDayRender="Calendar1_DayRender" OnVisibleMonthChanged="Calendar1_VisibleMonthChanged"> <DayHeaderStyle BackColor="Black" Font-Names="Arial Black" ForeColor="White" /> <SelectedDayStyle BackColor="Cornsilk" Font-Bold="True" Font-Italic="True" Font-Names="Arial" ForeColor="Blue" /> <SelectorStyle BackColor="Cornsilk" Font-Names="Arial" ForeColor="Red" /> <WeekendDayStyle BackColor="LavenderBlush" Font-Names="Arial" ForeColor="Purple" /> <OtherMonthDayStyle BackColor="LightGray" Font-Names="Arial" ForeColor="White" /> <TodayDayStyle BackColor="Cornsilk" Font-Bold="True" Font-Names="Arial" ForeColor="Green" /> <NextPrevStyle BackColor="DarkGray" Font-Names="Arial" ForeColor="Yellow" /> <TitleStyle BackColor="Gray" Font-Names="Arial Black" ForeColor="White" HorizontalAlign="Left" /> </asp:Calendar>
<asp:Label id="lblCount" runat="server" />
<asp:Label id="lblTodaysDate" runat="server" />
<asp:Label id="lblSelected" runat="server" />
Select a month: |
<asp:DropDownList id= "ddl" AutoPostBack="true" onSelectedIndexChanged = "ddl_SelectedIndexChanged" runat="server"> <asp:ListItem text="January" value="1" /> <asp:ListItem text="February" value="2" /> <asp:ListItem text="March" value="3" /> <asp:ListItem text="May" value="5" /> <asp:ListItem text="June" value="6" /> <asp:ListItem text="July" value="7" /> <asp:ListItem text="August" value="8" /> <asp:ListItem text="September" value="9" /> <asp:ListItem text="October" value="10" /> <asp:ListItem text="November" value="11" /> <asp:ListItem text="December" value="12" /> </asp:DropDownList> |
<asp:Button id="btnTgif" text="TGIF" onClick="btnTgif_Click" runat="server" /> |
Day Range | ||
Starting Day | Ending Day | |
<asp:TextBox id= "txtStart" runat="server" Width="25" MaxLength="2" /> |
<asp:TextBox id= "txtEnd" runat="server" Width="25" MaxLength="2" /> |
<asp:Button id="btnRange" runat="server" text="Apply" onClick="btnRange_Click" /> |
</form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Calendar1.VisibleDate = Calendar1.TodaysDate; ddl.SelectedIndex = Calendar1.VisibleDate.Month - 1; } lblTodaysDate.Text = "Today"s Date is " + Calendar1.TodaysDate.ToShortDateString(); } protected void Calendar1_SelectionChanged(object sender, EventArgs e) { lblSelectedUpdate(); lblCountUpdate(); txtClear(); } private void lblSelectedUpdate() { if (Calendar1.SelectedDate != DateTime.MinValue) lblSelected.Text = "The date selected is " + Calendar1.SelectedDate.ToShortDateString(); } private void lblCountUpdate() { lblCount.Text = "Count of Days Selected: " + Calendar1.SelectedDates.Count.ToString(); } protected void ddl_SelectedIndexChanged(Object sender, EventArgs e) { Calendar1.SelectedDates.Clear(); lblSelectedUpdate(); lblCountUpdate(); Calendar1.VisibleDate = new DateTime(Calendar1.VisibleDate.Year, Int32.Parse(ddl.SelectedItem.Value), 1); txtClear(); } protected void btnTgif_Click(Object sender, EventArgs e) { int currentMonth = Calendar1.VisibleDate.Month; int currentYear = Calendar1.VisibleDate.Year; Calendar1.SelectedDates.Clear(); for (int i = 1; i <= System.DateTime.DaysInMonth(currentYear,currentMonth); i++) { DateTime date = new DateTime(currentYear, currentMonth, i); if (date.DayOfWeek == DayOfWeek.Friday) Calendar1.SelectedDates.Add(date); } lblSelectedUpdate(); lblCountUpdate(); txtClear(); } protected void btnRange_Click(Object sender, EventArgs e) { int currentMonth = Calendar1.VisibleDate.Month; int currentYear = Calendar1.VisibleDate.Year; DateTime StartDate = new DateTime(currentYear, currentMonth, Int32.Parse(txtStart.Text)); DateTime EndDate = new DateTime(currentYear, currentMonth, Int32.Parse(txtEnd.Text)); Calendar1.SelectedDates.Clear(); Calendar1.SelectedDates.SelectRange(StartDate, EndDate); lblSelectedUpdate(); lblCountUpdate(); } private void txtClear() { txtStart.Text = ""; txtEnd.Text = ""; } protected void Calendar1_DayRender(object sender, DayRenderEventArgs e) { if (!e.Day.IsOtherMonth && e.Day.IsWeekend) e.Cell.BackColor = System.Drawing.Color.LightGreen; if (e.Day.Date.Month == 1 && e.Day.Date.Day == 1) e.Cell.Controls.Add(new LiteralControl("
Happy New Year!")); } protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e) { if ((e.NewDate.Year > e.PreviousDate.Year) || ((e.NewDate.Year == e.PreviousDate.Year) && (e.NewDate.Month > e.PreviousDate.Month))) lblMonthChanged.Text = "My future"s so bright..."; else lblMonthChanged.Text = "Back to the future!"; Calendar1.SelectedDates.Clear(); lblSelectedUpdate(); lblCountUpdate(); txtClear(); }
}
</source>
Define and use calendar in code behind (C#)
<source lang="csharp">
<%@ Page language="c#" src="Calendar.aspx.cs" AutoEventWireup="false" Inherits="MyOwnCalendar" %> <HTML>
<body> <form id="Form1" method="post" runat="server"> <asp:Calendar id="MyCalendar" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 24px" runat="server" Width="293px" Height="226px"></asp:Calendar> <asp:Label id="lblDates" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 272px" runat="server" Width="544px" Height="72px" Font-Bold="True" Font-Names="Verdana" Font-Size="Small"></asp:Label></form> </body>
</HTML>
<%-- using System; using System.Collections; using System.ruponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
public class MyOwnCalendar : System.Web.UI.Page { protected System.Web.UI.WebControls.Calendar MyCalendar; protected System.Web.UI.WebControls.Label lblDates; private void Page_Load(object sender, System.EventArgs e) { } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.MyCalendar.SelectionChanged += new System.EventHandler(this.MyCalendar_SelectionChanged); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void MyCalendar_SelectionChanged(object sender, System.EventArgs e) { lblDates.Text = "You selected these dates:
"; foreach (DateTime dt in MyCalendar.SelectedDates) { lblDates.Text += dt.ToLongDateString() + "
"; } } }
--%>
</source>
Displaying a Date in the Calendar Control (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %> <script runat=server> Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
If Not IsPostBack Then MyCal.SelectedDate = DateAdd( _ Microsoft.VisualBasic.DateInterval.Month, 2, Today()) MyCal.VisibleDate = DateAdd( _ Microsoft.VisualBasic.DateInterval.Month, 2, Today()) End If
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Displaying a Date in the Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<asp:Calendar
id="Mycal" runat="server" cellpadding="3" cellspacing="3"
/> </form> </BODY> </HTML>
</source>
Displaying a Selected Range of Dates in the Calendar Control (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %> <script runat=server> Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
If Not IsPostBack Then Dim I as Integer For i = 0 to 4 MyCal.SelectedDates.Add(DateAdd( _ Microsoft.VisualBasic.DateInterval.Day, _ i, Today())) Next End If
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Displaying a Selected Range of Dates in the Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<asp:calendar
id="Mycal" runat="server" cellpadding="3" cellspacing="3" selectionmode="DayWeekMonth" selectorstyle-font-bold="True" selectorStyle-backcolor="#3366ff" font-name="Tahoma" font-size="12" backcolor="ivory" selecteddaystyle-backcolor="#ffcc66" selecteddaystyle-font-bold="True"
/> </form> </BODY> </HTML>
</source>
Embed Javascript to C# code to create a popup window
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Select Date</title> <script language="javascript"> function SetDate(dateToSet) { controlName = window.location.search.substr(1).substring(8); window.opener.document.forms[0].elements[controlName].value = dateToSet; self.close(); } </script>
</head> <body>
<form id="form1" runat="server">
<asp:Calendar ID="calDefault" runat="server" BackColor="White" BorderColor="#999999" CellPadding="4" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="100%" Width="100%" OnDayRender="calDefault_DayRender" > <SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" /> <SelectorStyle BackColor="#CCCCCC" /> <WeekendDayStyle BackColor="#FFFFCC" /> <OtherMonthDayStyle ForeColor="#808080" /> <TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" /> <NextPrevStyle VerticalAlign="Bottom" /> <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" /> <TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" /> </asp:Calendar>
</form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Default : System.Web.UI.Page {
protected void calDefault_DayRender(object sender, DayRenderEventArgs e) { HyperLink link = new HyperLink(); LiteralControl lc = (LiteralControl)e.Cell.Controls[0]; link.Text = lc.Text; link.NavigateUrl = "javascript:SetDate("" + e.Day.Date.ToShortDateString() + "");"; e.Cell.Controls.Clear(); e.Cell.Controls.Add(link); }
}
</source>
Formatting Date Sections in a Calendar Control (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Formatting Date Sections in a Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<asp:Calendar
id="Mycal" runat="server" cellpadding="3" cellspacing="3" daynameformat="Short" firstdayofweek="Default" nextprevformat="FullMonth" titleformat="MonthYear" font-name="Tahoma" font-size="12" backcolor="ivory" todaydaystyle-Font-Bold="True" todaydaystyle-backcolor="Yellow" selecteddayStyle-backcolor="#ffcc66" selecteddayStyle-font-bold="True" weekenddaystyle-font-italic="True" weekenddaystyle-forecolor="Red" othermonthdaystyle-font-italic="True" othermonthdaystyle-backcolor="white" othermonthdaystyle-forecolor="gray"
/> </form> </BODY> </HTML>
</source>
Formatting the Header Styles in a Calendar Control (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %>
<script runat=server>
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Formatting the Header Styles in a Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<asp:Calendar
id="Mycal" runat="server" cellpadding="3" cellspacing="3" daynameformat="Short" firstdayofweek="Default" nextprevformat="FullMonth" titleformat="MonthYear" font-name="Tahoma" font-size="12" backcolor="ivory" dayheaderstyle-font-bold="True" dayheaderstyle-font-italic="True" dayheaderstyle-forecolor="DarkBlue" titlestyle-backcolor="#3366ff" titlestyle-forecolor="white" titlestyle-font-bold="True" nextprevstyle-font-italic="True" nextprevstyle-backcolor="Yellow"
/> </form> </BODY> </HTML>
</source>
Get selected date from asp:Calendar (VB.net)
<source lang="csharp">
<%@ Page Language="VB" %> <script runat="server">
Sub Calendar1_SelectionChanged(sender As Object, e As EventArgs) If Day (Calendar1.SelectedDate) = 13 And Calendar1.SelectedDate.DayOfWeek = 5 Then Label1.Text = "Careful! Friday 13th is an unlucky day for some people!" Else Label1.Text = "It"s just another day in life..." End If End Sub
</script> <html> <head> </head> <body>
<form runat="server">
Please select a date:
<asp:Calendar id="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
<asp:Label id="Label1" runat="server"></asp:Label>
</form>
</body> </html>
</source>
Load event in calendar selection event (C#)
<source lang="csharp">
<%@ Page language="c#" src="Appointment.aspx.cs" AutoEventWireup="false" Inherits="Appointment" %> <HTML>
<body> <form id="Form1" method="post" runat="server"> <asp:Calendar id="Calendar1" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 16px" runat="server" SelectionMode="DayWeekMonth" BorderWidth="1px" BackColor="#FFFFCC" Width="220px" DayNameFormat="FirstLetter" ForeColor="#663399" Height="200px" Font-Size="8pt" Font-Names="Verdana" BorderColor="#FFCC66" ShowGridLines="True"> <TodayDayStyle ForeColor="White" BackColor="#FFCC66"></TodayDayStyle> <SelectorStyle BackColor="#FFCC66"></SelectorStyle> <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC"></NextPrevStyle> <DayHeaderStyle Height="1px" BackColor="#FFCC66"></DayHeaderStyle> <SelectedDayStyle Font-Bold="True" BackColor="#CCCCFF"></SelectedDayStyle> <TitleStyle Font-Size="9pt" Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></TitleStyle> <OtherMonthDayStyle ForeColor="#CC9966"></OtherMonthDayStyle> </asp:Calendar> <asp:ListBox id="lstTimes" style="Z-INDEX: 102; LEFT: 24px; POSITION: absolute; TOP: 248px" runat="server" Width="216px" Height="160px"></asp:ListBox> </form> </body>
</HTML>
<%-- Appointment.aspx.cs using System; using System.Collections; using System.ruponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
public class Appointment : System.Web.UI.Page { protected System.Web.UI.WebControls.Calendar Calendar1; protected System.Web.UI.WebControls.ListBox lstTimes; private void Page_Load(object sender, System.EventArgs e) { } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Calendar1.DayRender += new System.Web.UI.WebControls.DayRenderEventHandler(this.Calendar1_DayRender); this.Calendar1.SelectionChanged += new System.EventHandler(this.Calendar1_SelectionChanged); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e) { // Check for May 5 in any year, and format it. if (e.Day.Date.Day == 5 && e.Day.Date.Month == 5) { e.Cell.BackColor = System.Drawing.Color.Yellow; // Add some static text to the cell. Label lbl = new Label(); lbl.Text = "
My Birthday!"; e.Cell.Controls.Add(lbl); } } private void Calendar1_SelectionChanged(object sender, System.EventArgs e) { lstTimes.Items.Clear(); switch (Calendar1.SelectedDate.DayOfWeek) { case DayOfWeek.Monday: // Apply special Monday schedule. lstTimes.Items.Add("10:00 >> working"); lstTimes.Items.Add("10:30 >> Meeting"); lstTimes.Items.Add("11:00 >> Coding"); break; default: lstTimes.Items.Add("10:00 >> Coding"); lstTimes.Items.Add("10:30"); lstTimes.Items.Add("11:00"); lstTimes.Items.Add("11:30"); lstTimes.Items.Add("12:00 >> lauching"); lstTimes.Items.Add("12:30"); break; } } }
--%>
</source>
On Date and Month Selection Changed (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %> <%@ Import Namespace="System.Data" %> <script runat=server> Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
MyCal.SelectedDate = Today()
End Sub Sub calSelectChange(ByVal Sender as Object, ByVal E as EventArgs)
lblMessage.Text = "You selected " _ & MyCal.SelectedDate
End Sub Sub calMonthChange(ByVal source As Object, _
ByVal e As MonthChangedEventArgs) lblMessage2.Text = "
You changed from month " _ & e.PreviousDate.Month & " to month " _ & e.NewDate.Month & "."
End Sub </SCRIPT> <HTML> <HEAD> <TITLE>Calendar 2 Control Sample Page</TITLE> </HEAD> <BODY > <form runat="server"> <asp:Calendar
id="Mycal" runat="server" CellPadding="3" CellSpacing="3" DayNameFormat="Short" FirstDayOfWeek="Default" NextPrevFormat="FullMonth" SelectionMode="Day" ShowDayHeader="True" ShowGridLines="False" ShowNextPrevMonth="True" ShowTitle="True" TitleFormat="MonthYear" Font-Name="Tahoma" Font-Size="12" BackColor="ivory" TodayDayStyle-Font-Bold="True" DayHeaderStyle-Font-Bold="True" OtherMonthDayStyle-ForeColor="gray" TitleStyle-BackColor="#3366ff" TitleStyle-ForeColor="white" TitleStyle-Font-Bold="True" SelectedDayStyle-BackColor="#ffcc66" SelectedDayStyle-Font-Bold="True" WeekendDayStyle-Font-Italic="True" NextPrevStyle-Font-Italic="True" OnSelectionChanged="calSelectChange" OnVisibleMonthChanged="calMonthChange"
/>
<asp:Label
id="lblMessage" runat="server" Font-Bold="True"
/><asp:Label
id="lblMessage2" runat="server" Font-Bold="True"
/> </Form> </BODY> </HTML>
</source>
On visible month changed event for a calendar control (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %> <script runat=server> Sub calMonthChange(ByVal source As Object, _
ByVal e As MonthChangedEventArgs) lblMessage.Text = "
You changed from month " _ & e.PreviousDate.Month & " and year " _ & e.PreviousDate.Year & " to month " _ & e.NewDate.Month & " and year " _ & e.NewDate.Year
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Writing Code that Fires when the Month Changes in a Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<asp:Calendar
id="Mycal" runat="server" cellpadding="3" cellspacing="3" onvisiblemonthchanged="calMonthChange"
/>
<asp:Label
id="lblMessage" runat="server" Font-Bold="True"
/> </form> </BODY> </HTML>
</source>
Popup calendar
<source lang="csharp">
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Popup Date Selector</title> <script language="javascript" type="text/javascript"> function PickDate(controlName) { var wnd = null; var settings = "width=300,height=200,location=no,menubar=no,toolbar=no,scrollbars=no,resizable=yes,status=yes"; var url = "Popup.aspx?control=" + controlName; wnd = window.open(url,"DatePopup",settings); if (wnd.focus){ wnd.focus(); } } </script>
</head> <body>
<form id="form1" runat="server">
Start Date: <asp:TextBox ID="txtDateStart" runat="server" Columns="10"></asp:TextBox> <asp:ImageButton ID="btnDateStart" runat="server" ImageUrl="~/images/cal_allday.gif" OnClientClick="PickDate("txtDateStart")" />
End Date: <asp:TextBox ID="txtDateEnd" runat="server" Columns="10"></asp:TextBox> <asp:ImageButton ID="btnDateEnd" runat="server" ImageUrl="~/images/cal_allday.gif" OnClientClick="PickDate("txtDateEnd")" />
Press enter key to continue. </form>
</body> </html>
</source>
Reading Selected Dates in a Date Range through the Calendar Control (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %> <script runat=server> Sub calSelectChange(ByVal Sender as Object, ByVal E as EventArgs)
Dim i as Integer lblMessage.Text = "You selected:" For i = 0 to MyCal.SelectedDates.Count - 1 lblMessage.Text = lblMessage.Text & "
" _ & MyCal.SelectedDates(i).ToShortDateString() Next
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Reading Selected Dates in a Date Range through the Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<asp:calendar
id="Mycal" runat="server" cellpadding="3" cellspacing="3" selectionmode="DayWeekMonth" selectorstyle-font-bold="True" selectorStyle-backcolor="#3366ff" font-name="Tahoma" font-size="12" backcolor="ivory" selecteddaystyle-backcolor="#ffcc66" selecteddaystyle-font-bold="True" onselectionchanged="calSelectChange"
/>
<asp:Label
id="lblMessage" runat="server" Font-Bold="True"
/> </form> </BODY> </HTML>
</source>
Select a week (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %> <%@ Import Namespace="System.Data" %> <script runat=server> Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
MyCal.SelectedDate = Today()
End Sub Sub calSelectChange(ByVal Sender as Object, ByVal E as EventArgs)
Dim i as Integer lblMessage.Text = "You selected:" For i = 0 to MyCal.SelectedDates.Count - 1 lblMessage.Text = lblMessage.Text & "
" _ & MyCal.SelectedDates(i).ToShortDateString() Next
End Sub </SCRIPT> <HTML> <HEAD> <TITLE>Calendar 3 Control Sample Page</TITLE> </HEAD> <BODY > <form runat="server"> <asp:Calendar
id="Mycal" runat="server" CellPadding="3" CellSpacing="3" DayNameFormat="Short" FirstDayOfWeek="Default" NextPrevFormat="FullMonth" SelectionMode="DayWeekMonth" ShowDayHeader="True" ShowGridLines="False" ShowNextPrevMonth="True" ShowTitle="True" TitleFormat="MonthYear" Font-Name="Tahoma" Font-Size="12" BackColor="ivory" TodayDayStyle-Font-Bold="True" DayHeaderStyle-Font-Bold="True" OtherMonthDayStyle-ForeColor="gray" TitleStyle-BackColor="#3366ff" TitleStyle-ForeColor="white" TitleStyle-Font-Bold="True" SelectedDayStyle-BackColor="#ffcc66" SelectedDayStyle-Font-Bold="True" WeekendDayStyle-Font-Italic="True" NextPrevStyle-Font-Italic="True" SelectorStyle-Font-Bold="True" SelectorStyle-BackColor="#3366ff" OnSelectionChanged="calSelectChange"
/>
<asp:Label
id="lblMessage" runat="server" Font-Bold="True"
/> </Form> </BODY> </HTML>
</source>
Selected value change envent for asp:Calendar (VB.net)
<source lang="csharp">
<%@ Page Language="VB" %> <script runat="server">
Sub Calendar1_SelectionChanged(sender As Object, e As EventArgs) If Day (Calendar1.SelectedDate) = 13 And Calendar1.SelectedDate.DayOfWeek = 5 Then Label1.Text = "Careful! Friday 13th is an unlucky day for some people!" Else Label1.Text = "It"s just another day in life..." End If End Sub
</script> <html> <head> </head> <body>
<form runat="server">
Please select a date:
<asp:Calendar id="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
<asp:Label id="Label1" runat="server"></asp:Label>
</form>
</body> </html>
</source>
Set asp calendar border (VB.net)
<source lang="csharp">
<%@Page Language="C#"%> <html> <body> <form runat="server"> <asp:calendar id="calTest" runat="server"
BackColor="#ffff80" BorderColor="#c0c0c0" BorderStyle="ridge" BorderWidth="5" />
</form> </body> </html>
</source>
Set DayStyle, NextPrevStyle, DayHeaderStyle, TitleStyle, WeekendDayStyle
<source lang="csharp">
<%@ Page Language="C#" %> <HTML>
<body> <script runat="server"> void PrettyCalendar_OnDayRender (object Sender, System.Web.UI.WebControls.DayRenderEventArgs e) { if (e.Day.Date == System.DateTime.Today) { e.Cell.Controls.Add(new LiteralControl("
")); e.Cell.Controls.Add(new LiteralControl("Today!")); } } </script> <form runat="server" ID="Form1"> <asp:Calendar ID="PrettyCalendar" Runat="server" OnDayRender="PrettyCalendar_OnDayRender"> <TodayDayStyle ForeColor="Red" BackColor="Aqua"></TodayDayStyle> <DayStyle Font-Bold="True" HorizontalAlign="Left" Height="90px" BorderWidth="1px" BorderStyle="Solid" BorderColor="Red" Width="100px" VerticalAlign="Top" BackColor="Yellow"></DayStyle> <NextPrevStyle ForeColor="Blue"/> <DayHeaderStyle Font-Size="Large" Font-Bold="True" BorderWidth="1px" ForeColor="Brown" BorderStyle="Solid" BorderColor="Black" Width="100px" BackColor="#00C0C0"></DayHeaderStyle> <TitleStyle Font-Size="Large" Font-Bold="True" BorderWidth="1px" BorderStyle="Solid" BorderColor="Black" BackColor="#80FFFF"></TitleStyle> <WeekendDayStyle BackColor="#C0C0C0"></WeekendDayStyle> </asp:Calendar> </form> </body>
</HTML>
</source>
Setting Custom Previous and Next Month Text in the Calendar Control (VB.net)
<source lang="csharp">
<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Setting Custom Previous and Next Month Text in the Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<asp:Calendar
id="Mycal" runat="server" cellpadding="3" cellspacing="3" nextprevformat="CustomText" nextmonthtext="Next" prevmonthtext="Prev"
/> </form> </BODY> </HTML>
</source>
Set TodayDayStyle: background color and foreground color
<source lang="csharp">
<%@ Page Language="C#" %> <HTML>
<body> <script runat="server"> void PrettyCalendar_OnDayRender (object Sender, System.Web.UI.WebControls.DayRenderEventArgs e) { if (e.Day.Date == System.DateTime.Today) { e.Cell.Controls.Add(new LiteralControl("
")); e.Cell.Controls.Add(new LiteralControl("Today!")); } } </script> <form runat="server" ID="Form1"> <asp:Calendar ID="PrettyCalendar" Runat="server" OnDayRender="PrettyCalendar_OnDayRender"> <TodayDayStyle ForeColor="Red" BackColor="Aqua"></TodayDayStyle> <DayStyle Font-Bold="True" HorizontalAlign="Left" Height="90px" BorderWidth="1px" BorderStyle="Solid" BorderColor="Red" Width="100px" VerticalAlign="Top" BackColor="Yellow"></DayStyle> <NextPrevStyle ForeColor="Blue"/> <DayHeaderStyle Font-Size="Large" Font-Bold="True" BorderWidth="1px" ForeColor="Brown" BorderStyle="Solid" BorderColor="Black" Width="100px" BackColor="#00C0C0"></DayHeaderStyle> <TitleStyle Font-Size="Large" Font-Bold="True" BorderWidth="1px" BorderStyle="Solid" BorderColor="Black" BackColor="#80FFFF"></TitleStyle> <WeekendDayStyle BackColor="#C0C0C0"></WeekendDayStyle> </asp:Calendar> </form> </body>
</HTML>
</source>
Visible month changed event
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Calendar</title>
</head> <body>
<form id="form1" runat="server">
<asp:Label id="lblMonthChanged" runat="server" /> <asp:Calendar ID="Calendar1" runat="server" SelectionMode="DayWeekMonth" CellPadding="7" CellSpacing="5" DayNameFormat="FirstTwoLetters" FirstDayOfWeek="Monday" NextMonthText="Next >" PrevMonthText="< Prev" ShowGridLines="True" DayStyle-BackColor="White" DayStyle-ForeColor="Black" DayStyle-Font-Names="Arial" OnSelectionChanged="Calendar1_SelectionChanged" OnDayRender="Calendar1_DayRender" OnVisibleMonthChanged="Calendar1_VisibleMonthChanged"> <DayHeaderStyle BackColor="Black" Font-Names="Arial Black" ForeColor="White" /> <SelectedDayStyle BackColor="Cornsilk" Font-Bold="True" Font-Italic="True" Font-Names="Arial" ForeColor="Blue" /> <SelectorStyle BackColor="Cornsilk" Font-Names="Arial" ForeColor="Red" /> <WeekendDayStyle BackColor="LavenderBlush" Font-Names="Arial" ForeColor="Purple" /> <OtherMonthDayStyle BackColor="LightGray" Font-Names="Arial" ForeColor="White" /> <TodayDayStyle BackColor="Cornsilk" Font-Bold="True" Font-Names="Arial" ForeColor="Green" /> <NextPrevStyle BackColor="DarkGray" Font-Names="Arial" ForeColor="Yellow" /> <TitleStyle BackColor="Gray" Font-Names="Arial Black" ForeColor="White" HorizontalAlign="Left" /> </asp:Calendar>
<asp:Label id="lblCount" runat="server" />
<asp:Label id="lblTodaysDate" runat="server" />
<asp:Label id="lblSelected" runat="server" />
Select a month: |
<asp:DropDownList id= "ddl" AutoPostBack="true" onSelectedIndexChanged = "ddl_SelectedIndexChanged" runat="server"> <asp:ListItem text="January" value="1" /> <asp:ListItem text="February" value="2" /> <asp:ListItem text="March" value="3" /> <asp:ListItem text="May" value="5" /> <asp:ListItem text="June" value="6" /> <asp:ListItem text="July" value="7" /> <asp:ListItem text="August" value="8" /> <asp:ListItem text="September" value="9" /> <asp:ListItem text="October" value="10" /> <asp:ListItem text="November" value="11" /> <asp:ListItem text="December" value="12" /> </asp:DropDownList> |
<asp:Button id="btnTgif" text="TGIF" onClick="btnTgif_Click" runat="server" /> |
Day Range | ||
Starting Day | Ending Day | |
<asp:TextBox id= "txtStart" runat="server" Width="25" MaxLength="2" /> |
<asp:TextBox id= "txtEnd" runat="server" Width="25" MaxLength="2" /> |
<asp:Button id="btnRange" runat="server" text="Apply" onClick="btnRange_Click" /> |
</form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Calendar1.VisibleDate = Calendar1.TodaysDate; ddl.SelectedIndex = Calendar1.VisibleDate.Month - 1; } lblTodaysDate.Text = "Today"s Date is " + Calendar1.TodaysDate.ToShortDateString(); } protected void Calendar1_SelectionChanged(object sender, EventArgs e) { lblSelectedUpdate(); lblCountUpdate(); txtClear(); } private void lblSelectedUpdate() { if (Calendar1.SelectedDate != DateTime.MinValue) lblSelected.Text = "The date selected is " + Calendar1.SelectedDate.ToShortDateString(); } private void lblCountUpdate() { lblCount.Text = "Count of Days Selected: " + Calendar1.SelectedDates.Count.ToString(); } protected void ddl_SelectedIndexChanged(Object sender, EventArgs e) { Calendar1.SelectedDates.Clear(); lblSelectedUpdate(); lblCountUpdate(); Calendar1.VisibleDate = new DateTime(Calendar1.VisibleDate.Year, Int32.Parse(ddl.SelectedItem.Value), 1); txtClear(); } protected void btnTgif_Click(Object sender, EventArgs e) { int currentMonth = Calendar1.VisibleDate.Month; int currentYear = Calendar1.VisibleDate.Year; Calendar1.SelectedDates.Clear(); for (int i = 1; i <= System.DateTime.DaysInMonth(currentYear,currentMonth); i++) { DateTime date = new DateTime(currentYear, currentMonth, i); if (date.DayOfWeek == DayOfWeek.Friday) Calendar1.SelectedDates.Add(date); } lblSelectedUpdate(); lblCountUpdate(); txtClear(); } protected void btnRange_Click(Object sender, EventArgs e) { int currentMonth = Calendar1.VisibleDate.Month; int currentYear = Calendar1.VisibleDate.Year; DateTime StartDate = new DateTime(currentYear, currentMonth, Int32.Parse(txtStart.Text)); DateTime EndDate = new DateTime(currentYear, currentMonth, Int32.Parse(txtEnd.Text)); Calendar1.SelectedDates.Clear(); Calendar1.SelectedDates.SelectRange(StartDate, EndDate); lblSelectedUpdate(); lblCountUpdate(); } private void txtClear() { txtStart.Text = ""; txtEnd.Text = ""; } protected void Calendar1_DayRender(object sender, DayRenderEventArgs e) { if (!e.Day.IsOtherMonth && e.Day.IsWeekend) e.Cell.BackColor = System.Drawing.Color.LightGreen; if (e.Day.Date.Month == 1 && e.Day.Date.Day == 1) e.Cell.Controls.Add(new LiteralControl("
Happy New Year!")); } protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e) { if ((e.NewDate.Year > e.PreviousDate.Year) || ((e.NewDate.Year == e.PreviousDate.Year) && (e.NewDate.Month > e.PreviousDate.Month))) lblMonthChanged.Text = "My future"s so bright..."; else lblMonthChanged.Text = "Back to the future!"; Calendar1.SelectedDates.Clear(); lblSelectedUpdate(); lblCountUpdate(); txtClear(); }
}
</source>