ASP.NET 自定义Calendar Web 服务器控件
2008-10-30 20:43
默认情况下,Calendar 控件中的日显示为数字。如果启用日选择,则数字将显示为链接。有关详细信息,请参见如何:控制 Calendar Web 服务器控件中的用户日期选定 。
但是,您可以自定义个别日的外观和内容,这包括执行下面的操作:
当 Calendar 控件创建要发送到浏览器的输出时,它将引发 DayRender 事件。控件在准备要显示的日时将为每个日引发该事件,然后您可采用编程的方式检查正显示的是哪个日期,并对其进行适当的自定义。
DayRender 事件的方法带有两个参数,包括对引发事件的控件(Calendar 控件)的引用和一个 DayRenderEventArgs 类型的对象。DayRenderEventArgs 对象提供对另外两个对象的访问:
自定义个别日的外观
创建用于处理 Calendar 控件的 DayRender 事件的方法。
在该方法中,设置 Cell 对象的属性,您可以使用 DayRenderEventArgs 参数访问此对象。
下面的示例演示如何更改个别日的外观。该方法使日历中的节假日呈现为黄色,而周末呈现为绿色。在该示例中,节假日是 2005 年 11 月 23 日至 11 月 30 日。
Protected Sub Calendar1_DayRender(ByVal sender As Object , _
ByVal e As DayRenderEventArgs) Handles Calendar1.DayRender
' Display vacation dates in yellow boxes with purple borders.
Dim vacationStyle As New Style()
With vacationStyle
.BackColor = System.Drawing.Color.Yellow
.BorderColor = System.Drawing.Color.Purple
.BorderWidth = New Unit(3)
End With
' Display weekend dates in green boxes.
Dim weekendStyle As New Style()
weekendStyle.BackColor = System.Drawing.Color.Green
' Vacation is from Nov 23, 2005 to Nov 30, 2005.
If ((e.Day.Date >= New Date (2005, 11, 23)) _
And (e.Day.Date <= New Date (2005, 11, 30))) Then
e.Cell.ApplyStyle(vacationStyle)
ElseIf (e.Day.IsWeekend) Then
e.Cell.ApplyStyle(weekendStyle)
End If
End Sub
protected void Calendar1_DayRender(object sender,
DayRenderEventArgs e)
{
// Display vacation dates in yellow boxes with purple borders.
Style vacationStyle = new Style();
vacationStyle.BackColor = System.Drawing.Color.Yellow;
vacationStyle.BorderColor = System.Drawing.Color.Purple;
vacationStyle.BorderWidth = 3;
// Display weekend dates in green boxes.
Style weekendStyle = new Style();
weekendStyle.BackColor = System.Drawing.Color.Green;
if ((e.Day.Date >= new DateTime(2000,11,23)) &&
(e.Day.Date <= new DateTime(2000,11,30)))
{
// Apply the vacation style to the vacation dates.
e.Cell.ApplyStyle(vacationStyle);
}
else if (e.Day.IsWeekend)
{
// Apply the weekend style to the weekend dates.
e.Cell.ApplyStyle(weekendStyle);
} }
指定可选定个别日
在用于 Calendar 控件的 DayRender 事件的方法中,通过从 Day 对象的 Date 属性中获取信息来确定哪一日被呈现。
将该日的 IsSelectable 属性设置为 true。
下面的示例演示如何将日期 2005 年 10 月 1 日设置为可选的,而所有其他日期都为不可选的。
Protected Sub Calendar1_DayRender(ByVal sender As Object , _
ByVal e As DayRenderEventArgs) Handles Calendar1.DayRender
Dim myAppointment As Date = New Date (2005, 10, 1)
If (e.Day.Date = myAppointment) Then
e.Day.IsSelectable = True
Else
e.Day.IsSelectable = False
End If
End Sub
protected void Calendar1_DayRender(object sender,
DayRenderEventArgs e)
{
DateTime myAppointment = new DateTime(2005, 10, 1);
if (e.Day.Date == myAppointment)
{
e.Day.IsSelectable = true ;
}
else
{
e.Day.IsSelectable = false ;
}
}
向个别日中添加内容
在用于 Calendar 控件的 DayRender 事件的处理程序中,将任何 HTML 或 ASP.NET Web 控件添加到来自于 DayRenderEventArgs 参数的 Day 对象的 Controls 集合中。
下面的示例显示假日。在页面加载过程中,将以二维数组创建假日列表。假日描述将被加载到与其日期对应的元素中。在 DayRender 事件的方法中,将每个日与假日数组进行比较。如果对应的假日数组元素中包含值,则使用假日文本创建一个 Label 控件,并将其添加到该日的 Controls 集合中。
Dim holidays(13, 32) As String
Protected Sub Page_Load(ByVal sender As Object , _
ByVal e As System.EventArgs) Handles Me .Load
holidays(1, 1) = "Birthday"
holidays(2, 14) = "Anniversary"
End Sub
Protected Sub Calendar1_DayRender(ByVal sender As Object , _
ByVal e As DayRenderEventArgs) Handles Calendar1.DayRender
If e.Day.IsOtherMonth Then
e.Cell.Controls.Clear()
Else
Dim aDate As Date = e.Day.Date
Dim aHoliday As String = holidays(aDate.Month, aDate.Day)
If (Not aHoliday Is Nothing ) Then
Dim aLabel As Label = New Label()
aLabel.Text = "<br>" & aHoliday
e.Cell.Controls.Add(aLabel)
End If
End If
End Sub
string [,] holidays = new String[13, 32];
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
string aHoliday;
DateTime theDate = e.Day.Date;
aHoliday = holidays[theDate.Month, theDate.Day];
if (aHoliday != null )
{
Label aLabel = new Label();
aLabel.Text = " <br>" + aHoliday;
e.Cell.Controls.Add(aLabel);
}
}
protected void Page_Load(object sender, EventArgs e)
{
holidays[1, 1] = "Birthday" ;
holidays[2, 14] = "Anniversary" ;
}
自定义个别日的链接文本
在用于 Calendar 控件的 DayRender 事件的方法中,获取 DayRenderEventArgs 参数的 SelectUrl 属性。SelectUrl 属性将返回 JavaScript ,通常为该日呈现此 JavaScript 以引起指示日期选择的回发。
使用串联创建用 SelectUrl 属性的值作为 href 属性的 HTML 超链接。
将超链接添加为 Cell 对象的 Text 属性。
下面的示例显示假日。在页面加载过程中,将以二维数组创建假日列表。假日描述将被加载到与其日期对应的元素中。在 DayRender 事件的方法中,将每个日与假日数组进行比较。如果对应的假日数组元素包含值,则代码将创建显示假日名而非日编号的链接文本。
Visual Basic
Dim holidays(13, 32) As String
Protected Sub Page_Load(ByVal sender As Object , _
ByVal e As System.EventArgs) Handles Me .Load
holidays(1, 1) = "Birthday"
holidays(2, 14) = "Anniversary"
End Sub
Protected Sub Calendar1_DayRender(ByVal sender As Object , _
ByVal e As DayRenderEventArgs) Handles Calendar1.DayRender
If e.Day.IsOtherMonth Then
e.Cell.Controls.Clear()
Else
Dim aDate As Date = e.Day.Date
Dim aHoliday As String = holidays(aDate.Month, aDate.Day)
If (Not aHoliday Is Nothing ) Then
e.Cell.Text = _
"<a href=" & e.SelectUrl & ">" & aHoliday & "</a>"
End If
End If
End Sub
string [,] holidays = new String[13, 32];
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
string aHoliday;
DateTime theDate = e.Day.Date;
aHoliday = holidays[theDate.Month, theDate.Day];
if (aHoliday != null )
{
e.Cell.Text = "<a href=" + e.SelectUrl + ">" +
aHoliday + "</a>" ;
}
}
protected void Page_Load(object sender, EventArgs e)
{
holidays[1, 1] = "Birthday" ;
holidays[2, 14] = "Anniversary" ;
}