Calendar.OnVisibleMonthChanged(DateTime, DateTime) 方法

定義

引發 VisibleMonthChanged 控制項的 Calendar 事件,並允許您提供 VisibleMonthChanged 事件的自訂處理常式。

protected:
 virtual void OnVisibleMonthChanged(DateTime newDate, DateTime previousDate);
protected virtual void OnVisibleMonthChanged (DateTime newDate, DateTime previousDate);
abstract member OnVisibleMonthChanged : DateTime * DateTime -> unit
override this.OnVisibleMonthChanged : DateTime * DateTime -> unit
Protected Overridable Sub OnVisibleMonthChanged (newDate As DateTime, previousDate As DateTime)

參數

newDate
DateTime

DateTime,表示目前在 Calendar 中顯示的月份。

previousDate
DateTime

DateTime,表示由 Calendar 顯示的上個月份。

範例

下列程式碼範例示範如何指定 和 撰寫事件處理常式 VisibleMonthChanged 的程式碼,以顯示 是否已 Calendar 向前或向後移動一個月。

<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">
    <title> Calendar VisibleMonthChanged Example </title>
<script runat="server">

      void MonthChange(Object sender, MonthChangedEventArgs e) 
      {

         if (e.NewDate.Month > e.PreviousDate.Month)
         { 
            Message.Text = "You moved forward one month.";
         }
         else
         {
            Message.Text = "You moved backwards one month.";
         }

      }
         
   </script>
 
</head>
 
<body>
 
   <form id="form1" runat="server">

      <h3> Calendar VisibleMonthChanged Example </h3>
       
      Select a different month on the calendar. 
      <br /><br />
 
      <asp:Calendar id="Calendar1" runat="server"
           OnVisibleMonthChanged="MonthChange">

         <WeekendDayStyle BackColor="gray">
         </WeekendDayStyle>

      </asp:Calendar>

      <hr /> 

      <table border="1">

         <tr style="background-color:Silver">

            <th>

               Month navigation direction

            </th>
         </tr>

         <tr>

            <td>
           
               <asp:Label id="Message" 
                    Text="Starting month." 
                    runat="server"/>

            </td>

         </tr>

      </table>
                   
   </form>
         
</body>

</html>
<%@ Page Language="VB" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">
    <title> Calendar VisibleMonthChanged Example </title>
<script runat="server">

      Sub MonthChange(sender As Object, e As MonthChangedEventArgs) 

         If e.NewDate.Month > e.PreviousDate.Month Then
         
            Message.Text = "You moved forward one month."
         
         Else
         
            Message.Text = "You moved backwards one month."
       
         End If

      End Sub
         
   </script>
 
</head>
 
<body>
 
   <form id="form1" runat="server">

      <h3> Calendar VisibleMonthChanged Example </h3>
       
      Select a different month on the calendar. 
      <br /><br />
 
      <asp:Calendar id="Calendar1" runat="server"
           OnVisibleMonthChanged="MonthChange">

         <WeekendDayStyle BackColor="gray">
         </WeekendDayStyle>

      </asp:Calendar>

      <hr /> 

      <table border="1">

         <tr style="background-color:Silver">

            <th>

               Month navigation direction

            </th>
         </tr>

         <tr>

            <td>
           
               <asp:Label id="Message" 
                    Text="Starting Month." 
                    runat="server"/>

            </td>

         </tr>

      </table>
                   
   </form>
         
</body>

</html>
<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">
    <title> Calendar VisibleMonthChanged Example </title>
<script runat="server">

      void MonthChange(Object sender, MonthChangedEventArgs e) 
      {

         if (e.NewDate.Month > e.PreviousDate.Month)
         { 
            Message.Text = "You moved forward one month.";
         }
         else
         {
            Message.Text = "You moved backwards one month.";
         }

      }
         
   </script>
 
</head>
 
<body>
 
   <form id="form1" runat="server">

      <h3> Calendar VisibleMonthChanged Example </h3>
       
      Select a different month on the calendar. 
      <br /><br />
 
      <asp:Calendar id="Calendar1" runat="server"
           OnVisibleMonthChanged="MonthChange">

         <WeekendDayStyle BackColor="gray">
         </WeekendDayStyle>

      </asp:Calendar>

      <hr /> 

      <table border="1">

         <tr style="background-color:Silver">

            <th>

               Month navigation direction

            </th>
         </tr>

         <tr>

            <td>
           
               <asp:Label id="Message" 
                    Text="Starting month." 
                    runat="server"/>

            </td>

         </tr>

      </table>
                   
   </form>
         
</body>

</html>
<%@ Page Language="VB" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">
    <title> Calendar VisibleMonthChanged Example </title>
<script runat="server">

      Sub MonthChange(sender As Object, e As MonthChangedEventArgs) 

         If e.NewDate.Month > e.PreviousDate.Month Then
         
            Message.Text = "You moved forward one month."
         
         Else
         
            Message.Text = "You moved backwards one month."
       
         End If

      End Sub
         
   </script>
 
</head>
 
<body>
 
   <form id="form1" runat="server">

      <h3> Calendar VisibleMonthChanged Example </h3>
       
      Select a different month on the calendar. 
      <br /><br />
 
      <asp:Calendar id="Calendar1" runat="server"
           OnVisibleMonthChanged="MonthChange">

         <WeekendDayStyle BackColor="gray">
         </WeekendDayStyle>

      </asp:Calendar>

      <hr /> 

      <table border="1">

         <tr style="background-color:Silver">

            <th>

               Month navigation direction

            </th>
         </tr>

         <tr>

            <td>
           
               <asp:Label id="Message" 
                    Text="Starting Month." 
                    runat="server"/>

            </td>

         </tr>

      </table>
                   
   </form>
         
</body>

</html>

備註

VisibleMonthChanged當使用者按一下標題區段上的下一個或上一個月導覽元素時,就會引發事件。

引發事件會透過委派叫用此事件處理常式。 如需詳細資訊,請參閱 處理和引發事件

OnVisibleMonthChanged 方法也允許衍生類別處理事件,而不用附加委派。 這是在衍生類別中處理事件的慣用技巧。

給繼承者的注意事項

當在衍生類別中覆寫 OnVisibleMonthChanged(DateTime, DateTime) 時,請確定呼叫基底類別的 OnVisibleMonthChanged(DateTime, DateTime) 方法,使已註冊的委派能接收到事件。

適用於

另請參閱