一.返回指定日期前后的某一日期;
// Sets a DateTime to April 3, 2002 of the Gregorian calendar.
DateTime myDT = new DateTime( 2002, 4, 3, new GregorianCalendar() );
// Uses the default calendar of the InvariantCulture.
Calendar myCal = CultureInfo.InvariantCulture.Calendar;
// Displays the values of the DateTime.
Console.WriteLine( "April 3, 2002 of the Gregorian calendar:" );
DisplayValues( myCal, myDT );
// Adds 5 to every component of the DateTime.
myDT = myCal.AddYears( myDT, 5 );
myDT = myCal.AddMonths( myDT, 5 );
myDT = myCal.AddWeeks( myDT, 5 );
myDT = myCal.AddDays( myDT, 5 );
myDT = myCal.AddHours( myDT, 5 );
myDT = myCal.AddMinutes( myDT, 5 );
myDT = myCal.AddSeconds( myDT, 5 );
myDT = myCal.AddMilliseconds( myDT, 5 );
// Sets a DateTime to April 3, 2002 of the Gregorian calendar.
DateTime myDT = new DateTime( 2002, 4, 3, new GregorianCalendar() );
// Uses the default calendar of the InvariantCulture.
Calendar myCal = CultureInfo.InvariantCulture.Calendar;
// Displays the values of the DateTime.
Console.WriteLine( "April 3, 2002 of the Gregorian calendar:" );
DisplayValues( myCal, myDT );
// Adds 5 to every component of the DateTime.
myDT = myCal.AddYears( myDT, 5 );
myDT = myCal.AddMonths( myDT, 5 );
myDT = myCal.AddWeeks( myDT, 5 );
myDT = myCal.AddDays( myDT, 5 );
myDT = myCal.AddHours( myDT, 5 );
myDT = myCal.AddMinutes( myDT, 5 );
myDT = myCal.AddSeconds( myDT, 5 );
myDT = myCal.AddMilliseconds( myDT, 5 );
二.在 Calendar Web 服务器控件中自定义个别日
默认情况下,Calendar 控件中的日只显示为数字。(如果启用日选定,则数字将显示为链接。有关详细信息,请参见在 Calendar Web 服务器控件中控制用户日期选定。)但是,您可以自定义单个日的内容和外观,如下所示:
当 Calendar 控件正在创建要发送到浏览器的输出时,它将引发一个您可以处理的 DayRender 事件。控件在准备要显示的日时将为每个日调用您的方法,然后您可采用编程的方式检查正显示的是哪个日期,并对其进行适当的自定义。
DayRender 事件的方法带有两个参数,包括引发事件的控件(Calendar 控件)的引用和一个 DayRenderEvent 类型的对象。DayRenderEvent 对象提供对另外两个对象的访问:
自定义个别日的外观
' Visual BasicPrivate Sub Calendar1_DayRender(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.DayRenderEventArgs)
Handles Calendar1.DayRender' Additional code hereEnd Sub//
C#private void Calendar1_DayRender
(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{ // Additional code here}
' Visual Basic If (e.Day.IsToday) Then
e.Cell.BackColor = System.Drawing.Color.Red
End If//
C# if (e.Day.IsToday)
{
e.Cell.BackColor = System.Drawing.Color.Red;
}
下例显示一个简单但完整的方法,该方法阐释了如何更改个别日的外观。该方法使日历中的节假日呈现为黄色,而周末呈现为绿色。
' Visual BasicPublic Sub Calendar1_DayRender
(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.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, 2000 to Nov 30, 2000.
If ((e.Day.Date >= New Date(2000, 11, 23)) _
And (e.Day.Date <= New Date(2000, 11, 30))) Then
e.Cell.ApplyStyle(vacationStyle)
ElseIf (e.Day.IsWeekend) Then
e.Cell.ApplyStyle(weekendStyle)
End IfEnd Sub
// C#private void Calendar1_DayRender (object sender,
System.Web.UI.WebControls.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); }}