Calendar.VisibleDate 屬性

定義

取得或設定 DateTime 值,指定要在 Calendar 控制項上顯示的月份。

public:
 property DateTime VisibleDate { DateTime get(); void set(DateTime value); };
[System.ComponentModel.Bindable(true)]
public DateTime VisibleDate { get; set; }
[<System.ComponentModel.Bindable(true)>]
member this.VisibleDate : DateTime with get, set
Public Property VisibleDate As DateTime

屬性值

DateTime,指定要顯示在 Calendar 上的月份。 預設值為 DateTime.MinValue,其會顯示包含 所 TodaysDate 指定日期的月份。

屬性

範例

下列程式碼範例示範如何使用 VisibleDate 屬性,以程式設計方式設定值,以決定控制項中顯示的 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>
    <title>Calendar Example</title>
<script language="C#" runat="server">

      void ButtonClick(Object sender, EventArgs e) 
      {
         Calendar1.VisibleDate = new DateTime(Calendar1.TodaysDate.Year, 
                                              DropList1.SelectedIndex + 1, 
                                              1);
         Label1.Text = "The VisibleDate property is " + 
                       Calendar1.VisibleDate.ToShortDateString();
      }

   </script>

</head>     
<body>

   <form id="form1" runat="server">

      <h3>Calendar Example</h3>

      <asp:Calendar id="Calendar1" runat="server"  
           SelectionMode="None" 
           ShowGridLines="True">
 
         <SelectedDayStyle BackColor="Yellow"
                           ForeColor="Red">
         </SelectedDayStyle>

      </asp:Calendar>

      <hr /><br />

      Select the month to display: <br />

      <asp:DropDownList id="DropList1" runat="server">

         <asp:ListItem>1</asp:ListItem>
         <asp:ListItem>2</asp:ListItem>
         <asp:ListItem>3</asp:ListItem>
         <asp:ListItem>4</asp:ListItem>
         <asp:ListItem>5</asp:ListItem>
         <asp:ListItem>6</asp:ListItem>
         <asp:ListItem>7</asp:ListItem>
         <asp:ListItem>8</asp:ListItem>
         <asp:ListItem>9</asp:ListItem>
         <asp:ListItem>10</asp:ListItem>
         <asp:ListItem>11</asp:ListItem>
         <asp:ListItem>12</asp:ListItem>

      </asp:DropDownList>

      <asp:Button id="Button1" 
           Text="Submit"
           OnClick="ButtonClick"
           runat="server" />

      <br /><br />

      <asp:Label id="Label1" runat="server"/>

   </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>
    <title>Calendar Example</title>
<script language="VB" runat="server">

        Sub ButtonClick(sender As Object, e As EventArgs)
            Calendar1.VisibleDate = New DateTime(Calendar1.TodaysDate.Year, DropList1.SelectedIndex + 1, 1)
            Label1.Text = "The VisibleDate property is " & Calendar1.VisibleDate.ToShortDateString()
        End Sub 'ButtonClick

   </script>

</head>     
<body>

   <form id="form1" runat="server">

      <h3>Calendar Example</h3>

      <asp:Calendar id="Calendar1" runat="server"  
           SelectionMode="None" 
           ShowGridLines="True">
 
         <SelectedDayStyle BackColor="Yellow"
                           ForeColor="Red">
         </SelectedDayStyle>

      </asp:Calendar>

      <hr /><br />

      Select the month to display: <br />

      <asp:DropDownList id="DropList1" runat="server">

         <asp:ListItem>1</asp:ListItem>
         <asp:ListItem>2</asp:ListItem>
         <asp:ListItem>3</asp:ListItem>
         <asp:ListItem>4</asp:ListItem>
         <asp:ListItem>5</asp:ListItem>
         <asp:ListItem>6</asp:ListItem>
         <asp:ListItem>7</asp:ListItem>
         <asp:ListItem>8</asp:ListItem>
         <asp:ListItem>9</asp:ListItem>
         <asp:ListItem>10</asp:ListItem>
         <asp:ListItem>11</asp:ListItem>
         <asp:ListItem>12</asp:ListItem>

      </asp:DropDownList>

      <asp:Button id="Button1" 
           Text="Submit"
           OnClick="ButtonClick"
           runat="server" />

      <br /><br />

      <asp:Label id="Label1" runat="server"/>

   </form>
</body>
</html>

備註

VisibleDate使用 屬性,以程式設計方式設定要顯示在 控制項上的 Calendar 月份。 指定的日期可以是您想要顯示之月份中的任何日期,雖然通常使用月份的第一個日期。 這個屬性是使用 System.DateTime 物件來設定。

屬性 VisibleDate 會在引發事件之前 VisibleMonthChanged 更新。 您可以提供自訂 OnVisibleMonthChanged 事件處理常式來設定 VisibleDate 屬性,並覆寫自動產生的值。

適用於

另請參閱