Calendar一些常用方法

 

Calendar

Calendar 控件显示一个月的日历,用户可以从中选择日期。下面的示例演示如何使用一个简单的 Calendar 控件。

C# Calendar1.aspx
Run Sample View Source
 

日期选择模式

Calendar 支持四种日期选择模式,如下表所述。

模式说明
Day用户可以选择任意一天。
DayWeek用户可以选择一天或整个星期。
DayWeekMonth用户可以选择一天、整个星期或整个可见月份。
None禁止日期选择。


下面的示例用一个 Calendar 控件演示模式选择。

C# Calendar2.aspx
Run Sample View Source
<html>
<head>
    <script language="VB" runat="server">

        Sub Date_Selected(sender As Object, e As EventArgs) 
        
            Select (Calendar1.SelectedDates.Count) 
                Case 0:   'None
                    Label1.Text = "No dates are currently selected"
                Case 1:   'Day
                    Label1.Text = "The selected date is " & Calendar1.SelectedDate.ToShortDateString
                Case 7:   'Week
                    Label1.Text = "The selection is a week beginning " & Calendar1.SelectedDate.ToShortDateString
                Case Else:  'Month
                    Label1.Text = "The selection is a month beginning " & Calendar1.SelectedDate.ToShortDateString
            End Select
            
        End Sub

    </script>
</head>

<body>

    <h3><font face="Verdana">Selection Link Graphics</font></h3>
    <p>

    <form runat=server>

        <asp:Calendar id=Calendar1 runat="server"
            onselectionchanged="Date_Selected"
            DayNameFormat="Short"
            SelectionMode="DayWeekMonth"
            Font-Names="Verdana;Arial" Font-Size="12px"
            Height="180px" Width="230px"
            TodayDayStyle-Font-Bold="True"
            DayHeaderStyle-Font-Bold="True"
            OtherMonthDayStyle-ForeColor="gray"
            TitleStyle-BackColor="#3366ff"
            TitleStyle-ForeColor="white"
            TitleStyle-Font-Bold="True"
            SelectedDayStyle-BackColor="#ffcc66"
            SelectedDayStyle-Font-Bold="True"
            NextMonthText = "<img src='images/monthright.gif' border=0>"
            PrevMonthText = "<img src='images/monthleft.gif' border=0>"
            SelectorStyle-BackColor="#99ccff"
            SelectWeekText = "<img src='images/selweek.gif' border=0 οnmοuseοver=this.style.backgroundColor='#ffcc66' οnmοuseοut=this.style.backgroundColor='#99ccff'>"
            SelectMonthText = "<img src='images/selmonth.gif' border=0 οnmοuseοver=this.style.backgroundColor='#ffcc66' οnmοuseοut=this.style.backgroundColor='#99ccff'>"
            />

        <p>
        
        <asp:Label id=Label1 runat="server" />
        
    </form>
</body>
</html>

选择链接图形

Calendar 控件可以使用文本或图形作为其选择链接。下面的示例演示如何使用图形创建更美观的日历。

C# Calendar3.aspx
Run Sample View Source

 

选择链接文本

Calendar 控件也可以使用文本标签进行周或月的选择,如下面的示例所示。

C# Calendar4.aspx
Run Sample View Source

<html>
<head>
    <script language="C#" runat="server">

        void Date_Selected(object s, EventArgs e) {

            switch (Calendar1.SelectedDates.Count) {

                case (0):   //None
                    Label1.Text = "No dates are currently selected";
                    break;
                case (1):   //Day
                    Label1.Text = "The selected date is " + Calendar1.SelectedDate.ToShortDateString();
                    break;
                case (7):   //Week
                    Label1.Text = "The selection is a week beginning " + Calendar1.SelectedDate.ToShortDateString();
                    break;
                default:    //Month
                    Label1.Text = "The selection is a month beginning " + Calendar1.SelectedDate.ToShortDateString();
                    break;
            }
        }

    </script>
</head>

<body>

    <h3><font face="Verdana">Selection Link Text</font></h3>
    <p>

    <form runat=server>

        <p>
        <asp:Calendar id=Calendar1 runat="server"
            onselectionchanged="Date_Selected"
            DayNameFormat="Short"
            SelectionMode="DayWeekMonth"
            Font-Names="Verdana;Arial" Font-Size="12px"
            Height="180px" Width="230px"
            TodayDayStyle-Font-Bold="True"
            DayHeaderStyle-Font-Bold="True"
            OtherMonthDayStyle-ForeColor="gray"
            TitleStyle-BackColor="#3366ff"
            TitleStyle-ForeColor="white"
            TitleStyle-Font-Bold="True"
            SelectedDayStyle-BackColor="#ffcc66"
            SelectedDayStyle-Font-Bold="True"
            NextPrevFormat="ShortMonth"
            NextPrevStyle-ForeColor="white"
            NextPrevStyle-Font-Size="10px"
            SelectorStyle-BackColor="#99ccff"
            SelectorStyle-ForeColor="navy"
            SelectorStyle-Font-Size="9px"
            SelectWeekText = "week"
            SelectMonthText = "month"
            />

        <p>
        <asp:Label id=Label1 runat="server" />
    </form>
</body>
</html>

向日历添加自定义内容

可以通过在 OnDayRender 事件中添加内容来生成约会样式的日历。 OnDayRender 的两个参数是所呈现的 Day 及其 Cell 对象。如下面的示例所示,可以通过将自定义文本作为 LiteralControl 添加到 Cell 对象的控件集合中来将该文本添加到特定一天的单元格中。

<script language="JavaScript" type="text/javascript"> function doClick(index, numTabs, id) { document.all("tab" + id, index).className = "tab"; for (var i=1; i < numTabs; i++) { document.all("tab" + id, (index + i) % numTabs).className = "backtab"; } document.all("code" + id, index).style.display = ""; for (var j=1; j < numTabs; j++) { document.all("code" + id, (index + j) % numTabs).style.display = "none"; } } </script>
C# VB  
		
string Hol = GetHoliday(Day.Date);

if (Hol != string.Empty) Cells.Controls.Add(new LiteralControl("<br>" + Hol));


C# Calendar5.aspx
Run Sample View Source
<html>
<head>
    <script language="C#" runat="server">

        String [][] holidays;

        void Page_Load(Object Sender, EventArgs e) {

            holidays = new String[13][];

            for (int n=0; n<13 ;n++)
                    holidays[n] = new String[32];

            holidays[1][1]   = "New Year's Day";
              holidays[1][26]  = "Australia Day";
            holidays[2][2]   = "Groundhog Day";
            holidays[2][14]  = "Valentine's Day";
            holidays[3][17]  = "St. Patrick's Day";
            holidays[4][1]   = "April Fool's Day";
            holidays[5][1]   = "May Day";
            holidays[6][15]  = "My Birthday";
            holidays[7][15]  = "My Anniversary";
            holidays[8][15]  = "My Mother's Birthday";
            holidays[9][24]  = "Autumnal Equinox";
            holidays[12][26] = "Boxing Day";
        }

        void Calendar1_DayRender(object sender, DayRenderEventArgs e) {

            CalendarDay d = ((DayRenderEventArgs)e).Day;
            TableCell c = ((DayRenderEventArgs)e).Cell;

            if (d.IsOtherMonth) {
                c.Controls.Clear();
            }
            else {
                try {
                    string Hol = holidays[d.Date.Month][d.Date.Day];

                    if (Hol != string.Empty)
                        c.Controls.Add(new LiteralControl("<br>" + Hol));
                }
                catch (Exception exc) {
                    Response.Write (exc.ToString());
                }
            }
        }

        void Date_Selected(object s, EventArgs e) {
            Label1.Text = "Selected date is: " + Calendar1.SelectedDate.ToShortDateString();
        }

    </script>

</head>

<body>

    <h3><font face="Verdana">Adding Custom Content to Calendar</font></h3>
    <p><p>

    <form runat=server>

        <asp:Calendar id=Calendar1 runat="server"
            ondayrender="Calendar1_DayRender"
            onselectionchanged="Date_Selected"
            ShowGridLines="true"
            Font-Names="Verdana;Arial"
            Font-Size="9px"
            Width="500px"
            VisibleDate="01/01/2000"
            TitleStyle-BackColor="Gainsboro"
            TitleStyle-Font-Size="12px"
            TitleStyle-Font-Bold="true"
            DayStyle-VerticalAlign="Top"
            DayStyle-Height="50px"
            DayStyle-Width="14%"
            SelectedDate="1/1/0001"
            SelectedDayStyle-BackColor="Navy"
            />

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

</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值