ASP.Net/Asp Control/Calendar — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:30, 26 мая 2010
Содержание
- 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 selection mode: DayWeek
- 15 Calendar selection mode: DayWeekMonth
- 16 Calendar with TodayDayStyle, SelectorStyle, NextPrevStyle, and SelectedDayStyle (C#)
- 17 Change Calendar selection based on data from the database (VB.net)
- 18 Change size of Calendar
- 19 Check the range of Calendar controls
- 20 Controlling the Appearance of Individual Cells of Dates (VB.net)
- 21 Convert selected date from asp:Calendar to long string (VB.net)
- 22 Date Selection event in a Calendar Control (VB.net)
- 23 Day renderer
- 24 Define and use calendar in code behind (C#)
- 25 Displaying a Date in the Calendar Control (VB.net)
- 26 Displaying a Selected Range of Dates in the Calendar Control (VB.net)
- 27 Embed Javascript to C# code to create a popup window
- 28 Formatting Date Sections in a Calendar Control (VB.net)
- 29 Formatting the Header Styles in a Calendar Control (VB.net)
- 30 Get selected date from asp:Calendar (VB.net)
- 31 Load event in calendar selection event (C#)
- 32 On Date and Month Selection Changed (VB.net)
- 33 On visible month changed event for a calendar control (VB.net)
- 34 Popup calendar
- 35 Reading Selected Dates in a Date Range through the Calendar Control (VB.net)
- 36 Select a week (VB.net)
- 37 Selected value change envent for asp:Calendar (VB.net)
- 38 Set asp calendar border (VB.net)
- 39 Set DayStyle, NextPrevStyle, DayHeaderStyle, TitleStyle, WeekendDayStyle
- 40 Setting Custom Previous and Next Month Text in the Calendar Control (VB.net)
- 41 Set TodayDayStyle: background color and foreground color
- 42 Visible month changed event
Add control to Calendar cell
<%@ 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("<br />"));
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>
Allowing the Selection of Multiple Dates in a Calendar Control (VB.net)
<%@ 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
<BR><BR>
<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>
Assign today"s date to asp calendar (VB.net)
<%@ 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">
<div>
<asp:Label
id="Label1"
Runat="server" />
<asp:Calendar
id="Calendar1"
TodayDayStyle-BackColor="Yellow"
Runat="server" />
</div>
</form>
</body>
</html>
Basic Calendar Control: show grid lines, show day header (VB.net)
<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Creating a Basic Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<BR><BR>
<asp:Calendar
id="Mycal"
runat="server"
cellpadding="3"
cellspacing="3"
showgridlines="true"
showdayheader="False"
shownextprevmonth="False"
showtitle="false"
/>
</form>
</BODY>
</HTML>
Calendar Control: day name format, first day of week (VB.net)
<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Creating a Basic Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<BR><BR>
<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>
Calendar control in code behind (C#)
<%@ Page language="c#" src="CalendarTest.aspx.cs" AutoEventWireup="false" Inherits="CalendarTest" %>
<HTML>
<body>
<form id="Form1" method="post" runat="server">
<P>
<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></P>
<P>
<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></P>
</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 = "<br>My Birthday!";
e.Cell.Controls.Add(lbl);
}
}
}
--%>
Calendar Control: nextprevformat (VB.net)
<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Creating a Basic Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<BR><BR>
<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>
Calendar control: on day render (VB.net)
<%@ 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
<BR><BR>
<asp:Calendar
id="Mycal"
runat="server"
cellpadding="3"
cellspacing="3"
ondayrender="DateDisplayed"
/>
</form>
</BODY>
</HTML>
Calendar Control: titleformat (VB.net)
<%@ Page Language=VB Debug=true %>
<HTML>
<HEAD>
<TITLE>Creating a Basic Calendar Control</TITLE>
</HEAD>
<BODY LEFTMARGIN="40">
<form runat="server">
Select a Date
<BR><BR>
<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>
Calendar events: render and change events
<%@ 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">
<div>
<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>
<br/>
<asp:Label id="lblCount" runat="server" />
<br/>
<asp:Label id="lblTodaysDate" runat="server" />
<br/>
<asp:Label id="lblSelected" runat="server" />
<br/>
<table>
<tr>
<td>
Select a month:
</td>
<td>
<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>
</td>
<td>
<asp:Button
id="btnTgif"
text="TGIF"
onClick="btnTgif_Click"
runat="server" />
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><b>Day Range</b></td>
</tr>
<tr>
<td>Starting Day</td>
<td>Ending Day</td>
</tr>
<tr>
<td>
<asp:TextBox id= "txtStart" runat="server"
Width="25"
MaxLength="2" />
</td>
<td>
<asp:TextBox id= "txtEnd" runat="server"
Width="25"
MaxLength="2" />
</td>
<td>
<asp:Button id="btnRange" runat="server"
text="Apply"
onClick="btnRange_Click" />
</td>
</tr>
</table>
</div>
</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("<br/>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();
}
}
Calendar selected value changed event (VB.net)
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>
<html>
<head><title>Using the Calendar</title></head>
<body>
<h3>Using the Calendar</h3>
<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>
Calendar selection changed event
<%@ 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">
<div>
<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>
<br/>
<asp:Label id="lblCount" runat="server" />
<br/>
<asp:Label id="lblTodaysDate" runat="server" />
<br/>
<asp:Label id="lblSelected" runat="server" />
</div>
</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();
}
}
Calendar selection mode: Day
<%@ 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">
<div>
<h1>Calendar Control</h1>
<h2>SelectionMode</h2>
<asp:Calendar ID="Calendar1" runat="server" SelectionMode="Day"></asp:Calendar>
</div>
</form>
</body>
</html>
Calendar selection mode: DayWeek
<%@ 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">
<div>
<h1>Calendar Control</h1>
<h2>SelectionMode</h2>
<asp:Calendar ID="Calendar1" runat="server" SelectionMode="DayWeek"></asp:Calendar>
</div>
</form>
</body>
</html>
Calendar selection mode: DayWeekMonth
<%@ 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">
<div>
<h1>Calendar Control</h1>
<h2>SelectionMode</h2>
<asp:Calendar ID="Calendar1" runat="server" SelectionMode="DayWeekMonth"></asp:Calendar>
</div>
</form>
</body>
</html>
Calendar with TodayDayStyle, SelectorStyle, NextPrevStyle, and SelectedDayStyle (C#)
<%@ Page language="c#" src="CalendarTest.aspx.cs" AutoEventWireup="false" Inherits="CalendarTest" %>
<HTML>
<body>
<form id="Form1" method="post" runat="server">
<P>
<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></P>
<P>
<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></P>
</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 = "<br>My Birthday!";
e.Cell.Controls.Add(lbl);
}
}
}
--%>
Change Calendar selection based on data from the database (VB.net)
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>
<html>
<head><title>Calendar: Hire Dates</title></head>
<body>
<h3>Calendar: Hire Dates</h3>
<form runat="server">
<asp:ListBox id="lstEmployees" runat="server"
Rows="7"
Autopostback="True" /><br/>
<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>
Change size of Calendar
<%@ 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("<br />"));
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>
Check the range of Calendar controls
<%@ 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">
<h1>Find a Flight</h1>
<table>
<tr>
<td>Departure Date</td>
<td><asp:Calendar ID="DepartureDate" Runat="server">
<DayStyle Font-Size="X-Small"></DayStyle>
<TitleStyle Font-Bold="True"></TitleStyle>
</asp:Calendar></td>
</tr>
<tr>
<td>Return Date</td>
<td><asp:Calendar ID="ReturnDate" Runat="server">
<DayStyle Font-Size="X-Small"></DayStyle>
<TitleStyle Font-Bold="True"></TitleStyle>
</asp:Calendar></td>
</tr>
</table>
<asp:Button ID="SearchButton" Runat="server" Text="Search" OnClick="SearchButton_Click" />
<br>
<asp:Label ID="ResultLabel" Runat="server" />
</form>
</body>
</HTML>
Controlling the Appearance of Individual Cells of Dates (VB.net)
<%@ 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
<BR><BR>
<asp:Calendar
id="Mycal"
runat="server"
cellpadding="3"
cellspacing="3"
ondayrender="DateDisplayed"
/>
</form>
</BODY>
</HTML>
Convert selected date from asp:Calendar to long string (VB.net)
<%@ 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">
<p>
<asp:Calendar id="Calendar1" runat="server"></asp:Calendar>
</p>
<p>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
</p>
<p>
<asp:Label id="Label1" runat="server">Label</asp:Label>
</p>
</form>
</body>
</html>
Date Selection event in a Calendar Control (VB.net)
<%@ 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
<BR><BR>
<asp:Calendar
id="Mycal"
runat="server"
cellpadding="3"
cellspacing="3"
onselectionchanged="calSelectChange"
/>
<BR><BR>
<asp:Label
id="lblMessage"
runat="server"
Font-Bold="True"
/>
</form>
</BODY>
</HTML>
Day renderer
<%@ 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">
<div>
<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>
<br/>
<asp:Label id="lblCount" runat="server" />
<br/>
<asp:Label id="lblTodaysDate" runat="server" />
<br/>
<asp:Label id="lblSelected" runat="server" />
<br/>
<table>
<tr>
<td>
Select a month:
</td>
<td>
<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>
</td>
<td>
<asp:Button
id="btnTgif"
text="TGIF"
onClick="btnTgif_Click"
runat="server" />
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><b>Day Range</b></td>
</tr>
<tr>
<td>Starting Day</td>
<td>Ending Day</td>
</tr>
<tr>
<td>
<asp:TextBox id= "txtStart" runat="server"
Width="25"
MaxLength="2" />
</td>
<td>
<asp:TextBox id= "txtEnd" runat="server"
Width="25"
MaxLength="2" />
</td>
<td>
<asp:Button id="btnRange" runat="server"
text="Apply"
onClick="btnRange_Click" />
</td>
</tr>
</table>
</div>
</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("<br/>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();
}
}
Define and use calendar in code behind (C#)
<%@ 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:<br>";
foreach (DateTime dt in MyCalendar.SelectedDates)
{
lblDates.Text += dt.ToLongDateString() + "<br>";
}
}
}
--%>
Displaying a Date in the Calendar Control (VB.net)
<%@ 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
<BR><BR>
<asp:Calendar
id="Mycal"
runat="server"
cellpadding="3"
cellspacing="3"
/>
</form>
</BODY>
</HTML>
Displaying a Selected Range of Dates in the Calendar Control (VB.net)
<%@ 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
<BR><BR>
<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>
Embed Javascript to C# code to create a popup window
<%@ 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">
<div>
<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>
</div>
</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);
}
}
Formatting Date Sections in a Calendar Control (VB.net)
<%@ 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
<BR><BR>
<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>
Formatting the Header Styles in a Calendar Control (VB.net)
<%@ 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
<BR><BR>
<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>
Get selected date from asp:Calendar (VB.net)
<%@ 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">
<p>
Please select a date:
</p>
<p>
<asp:Calendar id="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
</p>
<p>
<asp:Label id="Label1" runat="server"></asp:Label>
</p>
</form>
</body>
</html>
Load event in calendar selection event (C#)
<%@ 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 = "<br>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;
}
}
}
--%>
On Date and Month Selection Changed (VB.net)
<%@ 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 = "<BR>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">
<Font Face="Tahoma">
<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"
/>
<BR><BR>
<asp:Label
id="lblMessage"
runat="server"
Font-Bold="True"
/><asp:Label
id="lblMessage2"
runat="server"
Font-Bold="True"
/>
</Font>
</Form>
</BODY>
</HTML>
On visible month changed event for a calendar control (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub calMonthChange(ByVal source As Object, _
ByVal e As MonthChangedEventArgs)
lblMessage.Text = "<BR>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
<BR><BR>
<asp:Calendar
id="Mycal"
runat="server"
cellpadding="3"
cellspacing="3"
onvisiblemonthchanged="calMonthChange"
/>
<BR><BR>
<asp:Label
id="lblMessage"
runat="server"
Font-Bold="True"
/>
</form>
</BODY>
</HTML>
Popup calendar
<%@ 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">
<div>
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")" />
<br />
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")" />
</div>
Press enter key to continue.
</form>
</body>
</html>
Reading Selected Dates in a Date Range through the Calendar Control (VB.net)
<%@ 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 & "<BR>" _
& 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
<BR><BR>
<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"
/>
<BR><BR>
<asp:Label
id="lblMessage"
runat="server"
Font-Bold="True"
/>
</form>
</BODY>
</HTML>
Select a week (VB.net)
<%@ 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 & "<BR>" _
& MyCal.SelectedDates(i).ToShortDateString()
Next
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Calendar 3 Control Sample Page</TITLE>
</HEAD>
<BODY >
<form runat="server">
<Font Face="Tahoma">
<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"
/>
<BR><BR>
<asp:Label
id="lblMessage"
runat="server"
Font-Bold="True"
/>
</Font>
</Form>
</BODY>
</HTML>
Selected value change envent for asp:Calendar (VB.net)
<%@ 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">
<p>
Please select a date:
</p>
<p>
<asp:Calendar id="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
</p>
<p>
<asp:Label id="Label1" runat="server"></asp:Label>
</p>
</form>
</body>
</html>
Set asp calendar border (VB.net)
<%@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>
Set DayStyle, NextPrevStyle, DayHeaderStyle, TitleStyle, WeekendDayStyle
<%@ 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("<br />"));
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>
Setting Custom Previous and Next Month Text in the Calendar Control (VB.net)
<%@ 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
<BR><BR>
<asp:Calendar
id="Mycal"
runat="server"
cellpadding="3"
cellspacing="3"
nextprevformat="CustomText"
nextmonthtext="Next"
prevmonthtext="Prev"
/>
</form>
</BODY>
</HTML>
Set TodayDayStyle: background color and foreground color
<%@ 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("<br />"));
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>
Visible month changed event
<%@ 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">
<div>
<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>
<br/>
<asp:Label id="lblCount" runat="server" />
<br/>
<asp:Label id="lblTodaysDate" runat="server" />
<br/>
<asp:Label id="lblSelected" runat="server" />
<br/>
<table>
<tr>
<td>
Select a month:
</td>
<td>
<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>
</td>
<td>
<asp:Button
id="btnTgif"
text="TGIF"
onClick="btnTgif_Click"
runat="server" />
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><b>Day Range</b></td>
</tr>
<tr>
<td>Starting Day</td>
<td>Ending Day</td>
</tr>
<tr>
<td>
<asp:TextBox id= "txtStart" runat="server"
Width="25"
MaxLength="2" />
</td>
<td>
<asp:TextBox id= "txtEnd" runat="server"
Width="25"
MaxLength="2" />
</td>
<td>
<asp:Button id="btnRange" runat="server"
text="Apply"
onClick="btnRange_Click" />
</td>
</tr>
</table>
</div>
</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("<br/>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();
}
}