vs2005中Calendar控件的一些使用

一.返回指定日期前后的某一日期;

// 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 对象提供对另外两个对象的访问:

  • Cell,它是一个 TableCell 对象,可用于设置个别日的外观。
  • Day,可用于查询关于呈现日的信息,控制是否可选定该日,以及将内容添加到日中。Day 对象支持各种可用于了解有关日的信息的属性(例如,IsSelectedIsToday 等)。它还支持 Controls 集合,可操作该集合以将内容添加到日中。

自定义个别日的外观

  1. Calendar 控件的 DayRender 事件创建一个方法。该事件应该具有以下签名:
    ' Visual Basic
    Private Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
    ' Additional code here
    End Sub
    
    // C#
    private void Calendar1_DayRender (object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
    {
       // Additional code here
    }
  2. 在该方法中,设置通过 DayRenderEvent 参数可用的 Cell 对象的属性,如下例所示:
    ' 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 Basic
Public 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 If
End 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);
   }
}   


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值