Calendar使用- 显示特定节日样式

asp.net2.0里用Calendar控件做带节日提示的日历

2007-9-4 17:01:07 已被阅读: 176 【 字号:

1、建立网页CalendarThree.aspx,代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CalendarThree.aspx.cs" Inherits="CalendarThree" %>

<!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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Calendar ID="Calendar1" runat="server" OnDayRender="Calendar1_DayRender" Height="206px" Width="369px"></asp:Calendar>

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

2、CalendarThree.aspx.cs代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CalendarThree : System.Web.UI.Page
{
String[][] holidays = new String[13][];
protected void Page_Load(object sender, EventArgs e)
{
for (int n = 0; n < 13; n++)
holidays[n] = new String[32];

holidays[1][1] = "元旦";
holidays[2][14] = "情人节";
holidays[3][8] = "妇女节";
holidays[3][12] = "植树节";
holidays[4][1] = "愚人节";
holidays[5][1] = "劳动节";
holidays[5][4] = "青年节";
holidays[5][12] = "护士节";
holidays[5][14] = "母亲节";
holidays[5][14] = "助残日";
holidays[6][1] = "国际儿童节";
holidays[6][5] = "环境保护日";
holidays[6][18] = "父亲节";
holidays[6][26] = "国际禁毒日";
holidays[7][1] = "中共诞辰";
holidays[8][1] = "建军节";
holidays[9][10] = "教师节";
holidays[10][1] = "国庆节";
holidays[11][23] = "感恩节";
holidays[12][1] = "艾滋病日";
holidays[12][12] = "西安事变";
holidays[12][25] = "圣诞节";
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
CalendarDay d = ((DayRenderEventArgs)e).Day;
TableCell c = ((DayRenderEventArgs)e).Cell;

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

if (Hol != string.Empty)
e.Cell.Controls.Add(new LiteralControl("<br><font color=blue size=2>" + Hol + "</font>"));
}
catch (Exception exc)
{
Response.Write(exc.ToString());
}
}
}
}

3、运行的效果如下:


----------------------------------------------------------------------
示例2、


Calendar控件的基本用法(实现日期格式变换和定制节日)

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    private string [,] holiday=new string[12,31];//定义一个数组显示一年365天
    protected void Page_Load(object sender, EventArgs e)
    {
        holiday[8, 2] = ”我靠,我成功了!";//令8月2日为特殊纪念日
    }
    //日期选择格式
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string a = this.DropDownList1.SelectedValue;//a列表中选定项的值
        switch (a)
        {
            case "day":
                this.Calendar1.SelectionMode=CalendarSelectionMode.Day;//Calendar控件上选择单个日期
                break;
            case "month":
                this.Calendar1.SelectionMode = CalendarSelectionMode.DayWeekMonth;//Calendar控件上选择单个日期,周或者月
                break;
            case "week":
                this.Calendar1.SelectionMode = CalendarSelectionMode.DayWeek;//Calendar控件上选择单个日期或者整周
                break;
            case "none":
                this.Calendar1.SelectionMode = CalendarSelectionMode.None;//Calendar控件上不能选择任何日期
                break;
        }
    }
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
       CalendarDay d;//d为控件中的日期
       TableCell c;//c为控件中的单元格
       d = e.Day;//获取Calendar控件中的日期的CalendarDay对象
       c = e.Cell;
       if (d.IsOtherMonth)
           c.Controls.Clear();//从当前服务器的System.Web.UI.ControlCollection对象中移除所有控件
       else
           try
           {
               string Hol;
               Hol = holiday[d.Date.Month,d.Date.Day];//获取此实例表示日期的月和日期
               if(Hol!="")
                   c.Controls.Add(new LiteralControl("<br> <font color=red>" + Hol+"</font>"));//将指定的System.Web.UI.Control对象添加到集合,并用指定的文本初始化System.Web.UI.LiteralControl类的新实例
           }
        catch(Exception exc)//捕获错误信息
           {
               Response.Write(exc.Message);
           }

    }


转载于:https://www.cnblogs.com/beyond0309/archive/2008/04/13/1151094.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值