Calendar 控件显示一个月的日历,用户可以从中选择日期。下面的示例演示如何使用一个简单的
Calendar 控件。
C# Calendar1.aspx
日期选择模式
Calendar 支持四种日期选择模式,如下表所述。
模式 | 说明 |
---|
Day | 用户可以选择任意一天。 |
DayWeek | 用户可以选择一天或整个星期。 |
DayWeekMonth | 用户可以选择一天、整个星期或整个可见月份。 |
None | 禁止日期选择。 |
下面的示例用一个
Calendar 控件演示模式选择。
C# Calendar2.aspx
<html>
<head>
<script language="VB" runat="server">
Sub Date_Selected(sender As Object, e As EventArgs)
Select (Calendar1.SelectedDates.Count)
Case 0: 'None
Label1.Text = "No dates are currently selected"
Case 1: 'Day
Label1.Text = "The selected date is " & Calendar1.SelectedDate.ToShortDateString
Case 7: 'Week
Label1.Text = "The selection is a week beginning " & Calendar1.SelectedDate.ToShortDateString
Case Else: 'Month
Label1.Text = "The selection is a month beginning " & Calendar1.SelectedDate.ToShortDateString
End Select
End Sub
</script>
</head>
<body>
<h3><font face="Verdana">Selection Link Graphics</font></h3>
<p>
<form runat=server>
<asp:Calendar id=Calendar1 runat="server"
onselectionchanged="Date_Selected"
DayNameFormat="Short"
SelectionMode="DayWeekMonth"
Font-Names="Verdana;Arial" Font-Size="12px"
Height="180px" Width="230px"
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"
NextMonthText = "<img src='images/monthright.gif' border=0>"
PrevMonthText = "<img src='images/monthleft.gif' border=0>"
SelectorStyle-BackColor="#99ccff"
SelectWeekText = "<img src='images/selweek.gif' border=0 onmouseover=this.style.backgroundColor='#ffcc66' onmouseout=this.style.backgroundColor='#99ccff'>"
SelectMonthText = "<img src='images/selmonth.gif' border=0 onmouseover=this.style.backgroundColor='#ffcc66' onmouseout=this.style.backgroundColor='#99ccff'>"
/>
<p>
<asp:Label id=Label1 runat="server" />
</form>
</body>
</html>
选择链接图形
Calendar 控件可以使用文本或图形作为其选择链接。下面的示例演示如何使用图形创建更美观的日历。
C# Calendar3.aspx
选择链接文本
Calendar 控件也可以使用文本标签进行周或月的选择,如下面的示例所示。
C# Calendar4.aspx
<html>
<head>
<script language="C#" runat="server">
void Date_Selected(object s, EventArgs e) {
switch (Calendar1.SelectedDates.Count) {
case (0): //None
Label1.Text = "No dates are currently selected";
break;
case (1): //Day
Label1.Text = "The selected date is " + Calendar1.SelectedDate.ToShortDateString();
break;
case (7): //Week
Label1.Text = "The selection is a week beginning " + Calendar1.SelectedDate.ToShortDateString();
break;
default: //Month
Label1.Text = "The selection is a month beginning " + Calendar1.SelectedDate.ToShortDateString();
break;
}
}
</script>
</head>
<body>
<h3><font face="Verdana">Selection Link Text</font></h3>
<p>
<form runat=server>
<p>
<asp:Calendar id=Calendar1 runat="server"
onselectionchanged="Date_Selected"
DayNameFormat="Short"
SelectionMode="DayWeekMonth"
Font-Names="Verdana;Arial" Font-Size="12px"
Height="180px" Width="230px"
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"
NextPrevFormat="ShortMonth"
NextPrevStyle-ForeColor="white"
NextPrevStyle-Font-Size="10px"
SelectorStyle-BackColor="#99ccff"
SelectorStyle-ForeColor="navy"
SelectorStyle-Font-Size="9px"
SelectWeekText = "week"
SelectMonthText = "month"
/>
<p>
<asp:Label id=Label1 runat="server" />
</form>
</body>
</html>
向日历添加自定义内容
可以通过在
OnDayRender 事件中添加内容来生成约会样式的日历。
OnDayRender 的两个参数是所呈现的
Day 及其
Cell 对象。如下面的示例所示,可以通过将自定义文本作为
LiteralControl 添加到
Cell 对象的控件集合中来将该文本添加到特定一天的单元格中。
C# | VB | |
string Hol = GetHoliday(Day.Date);
if (Hol != string.Empty) Cells.Controls.Add(new LiteralControl("<br>" + Hol));
Dim Hol As String = GetHoliday(Day.Date)
If Hol <> String.Empty Then Cells.Controls.Add(New LiteralControl("<br>" + Hol))
|
C# Calendar5.aspx
<html>
<head>
<script language="C#" runat="server">
String [][] holidays;
void Page_Load(Object Sender, EventArgs e) {
holidays = new String[13][];
for (int n=0; n<13 ;n++)
holidays[n] = new String[32];
holidays[1][1] = "New Year's Day";
holidays[1][26] = "Australia Day";
holidays[2][2] = "Groundhog Day";
holidays[2][14] = "Valentine's Day";
holidays[3][17] = "St. Patrick's Day";
holidays[4][1] = "April Fool's Day";
holidays[5][1] = "May Day";
holidays[6][15] = "My Birthday";
holidays[7][15] = "My Anniversary";
holidays[8][15] = "My Mother's Birthday";
holidays[9][24] = "Autumnal Equinox";
holidays[12][26] = "Boxing Day";
}
void Calendar1_DayRender(object sender, DayRenderEventArgs e) {
CalendarDay d = ((DayRenderEventArgs)e).Day;
TableCell c = ((DayRenderEventArgs)e).Cell;
if (d.IsOtherMonth) {
c.Controls.Clear();
}
else {
try {
string Hol = holidays[d.Date.Month][d.Date.Day];
if (Hol != string.Empty)
c.Controls.Add(new LiteralControl("<br>" + Hol));
}
catch (Exception exc) {
Response.Write (exc.ToString());
}
}
}
void Date_Selected(object s, EventArgs e) {
Label1.Text = "Selected date is: " + Calendar1.SelectedDate.ToShortDateString();
}
</script>
</head>
<body>
<h3><font face="Verdana">Adding Custom Content to Calendar</font></h3>
<p><p>
<form runat=server>
<asp:Calendar id=Calendar1 runat="server"
ondayrender="Calendar1_DayRender"
onselectionchanged="Date_Selected"
ShowGridLines="true"
Font-Names="Verdana;Arial"
Font-Size="9px"
Width="500px"
VisibleDate="01/01/2000"
TitleStyle-BackColor="Gainsboro"
TitleStyle-Font-Size="12px"
TitleStyle-Font-Bold="true"
DayStyle-VerticalAlign="Top"
DayStyle-Height="50px"
DayStyle-Width="14%"
SelectedDate="1/1/0001"
SelectedDayStyle-BackColor="Navy"
/>
<p>
<asp:Label id=Label1 runat="server" />
</form>
</body>
</html>