日历控制显示用户能选择日期的月日历。 使用简单的日历控制,以下样品说明。
calendar1_cs.aspx
calendar1_cs.aspx
<html>
<head>
<script language="C#" runat="server">
void Date_Selected(object s, EventArgs e) {
Label1.Text = "Selected date is: " + Calendar1.SelectedDate.ToShortDateString();
}
</script>
</head>
<body>
<h3><font face="Verdana">Calendar Example</font></h3>
<form runat=server>
<asp:Calendar id=Calendar1 onselectionchanged="Date_Selected" runat="server" />
<p>
<asp:Label id=Label1 runat="server" />
</form>
</body>
</html>
排进日程支持四个日期选择方式,正如下表所描述。
| 方式 | 描述 |
|---|---|
| Day | 用户能选择所有一个工作日。 |
| DayWeek | 用户能选择一一个工作日或者一个整个星期。 |
| DayWeekMonth | 用户能选择一一个工作日、一个整个星期或者整个可看见的月。 |
| None | 日期选择是残疾。 |
以下样品展示与日历控制的模式选择。
calendar2_cs.aspx
<html>
<head>
<script language="C#" runat="server">
void Page_Load(Object Sender, EventArgs e) {
Calendar1.SelectionMode = (CalendarSelectionMode)lstSelMode.SelectedIndex;
if (Calendar1.SelectionMode == CalendarSelectionMode.None)
Calendar1.SelectedDates.Clear();
}
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">Date Selection Modes</font></h3>
<p>
<form runat=server>
Choose a Selection Mode:
<asp:DropDownList id="lstSelMode" runat=server
AutoPostBack=true>
<asp:ListItem Value="None" >None</asp:ListItem>
<asp:ListItem Selected Value="Day" >Day</asp:ListItem>
<asp:ListItem Value="DayWeek" >DayWeek</asp:ListItem>
<asp:ListItem Value="DayWeekMonth" >DayWeekMonth</asp:ListItem>
</asp:DropDownList>
<p>
<asp:Calendar id=Calendar1 runat="server"
onselectionchanged="Date_Selected"
Font-Names="Arial" Font-Size="12px"
Height="180px" Width="200px"
SelectorStyle-BackColor="gainsboro"
TodayDayStyle-BackColor="gainsboro"
DayHeaderStyle-BackColor="gainsboro"
OtherMonthDayStyle-ForeColor="gray"
TitleStyle-BackColor="gray"
TitleStyle-Font-Bold="True"
TitleStyle-Font-Size="12px"
SelectedDayStyle-BackColor="Navy"
SelectedDayStyle-Font-Bold="True"
/>
<p>
<asp:Label id=Label1 runat="server" />
</form>
</body>
</html>
日历控制可能为它的选择链接使用文本或图表。 以下样品显示如何使用图表创造一本悦目的日历。
calendar3_cs.aspx
<html>
<head>
<script language="C#" runat="server">
void Date_Selected(object sender, 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 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>
选择链接文本
如以下例子所显示,日历控制可能为星期或月选择也使用文本标签。
calendar4_cs.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的论据是被回报的天和它的细胞对象。 如以下例子所显示,习惯文本可以增加到细胞一特殊天通过增加它, LiteralControl到细胞对象的控制汇集。
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))
calendar5_cs.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>
浙公网安备 33010602011771号