在此之前,我也做过一个VB对日历控件进行操作的例题,但是多少有点烦琐!
原理是一样的,地址是 http://thcjp.cnblogs.com/archive/2006/02/28/339622.html
下面是今天用#做的例题
前台HTML代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
控制日历的选择模式:<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="None">无选择</asp:ListItem>
<asp:ListItem Value="Day">天</asp:ListItem>
<asp:ListItem Value="DayWeek">天、周</asp:ListItem>
<asp:ListItem Value="DayWeekMonth">天、周、月</asp:ListItem>
</asp:DropDownList>
<asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66"
BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
ForeColor="#663399" Height="200px" NextMonthText="下月" NextPrevFormat="ShortMonth"
OnSelectionChanged="Calendar1_SelectionChanged" PrevMonthText="上月" SelectMonthText="选全月"
SelectWeekText="选全周" ShowGridLines="True" Width="328px" DayHeaderStyle-Font-Italic="true" OnDayRender="Calendar1_DayRender" OnVisibleMonthChanged="Calendar1_VisibleMonthChanged">
<SelectedDayStyle BackColor="#CCCCFF" BorderStyle="Double" Font-Bold="True" />
<TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
<SelectorStyle BackColor="#FFCC66" />
<OtherMonthDayStyle ForeColor="#CC9966" />
<NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
<DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" Font-Italic="True" />
<TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
<WeekendDayStyle BackColor="LightSalmon" />
</asp:Calendar>
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label><br />
选择的 月是:<asp:Label ID="Label2" runat="server"></asp:Label><br />
选择的 年是:<asp:Label ID="Label4" runat="server"></asp:Label><br />
<br />
显示输入的时间
<asp:DropDownList ID="ye" runat="server">
</asp:DropDownList>年<asp:DropDownList ID="mo" runat="server">
</asp:DropDownList>月<asp:DropDownList ID="da" runat="server">
</asp:DropDownList>日
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="切" Width="85px" /><br />
<br />
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
控制日历的选择模式:<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="None">无选择</asp:ListItem>
<asp:ListItem Value="Day">天</asp:ListItem>
<asp:ListItem Value="DayWeek">天、周</asp:ListItem>
<asp:ListItem Value="DayWeekMonth">天、周、月</asp:ListItem>
</asp:DropDownList>
<asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66"
BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
ForeColor="#663399" Height="200px" NextMonthText="下月" NextPrevFormat="ShortMonth"
OnSelectionChanged="Calendar1_SelectionChanged" PrevMonthText="上月" SelectMonthText="选全月"
SelectWeekText="选全周" ShowGridLines="True" Width="328px" DayHeaderStyle-Font-Italic="true" OnDayRender="Calendar1_DayRender" OnVisibleMonthChanged="Calendar1_VisibleMonthChanged">
<SelectedDayStyle BackColor="#CCCCFF" BorderStyle="Double" Font-Bold="True" />
<TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
<SelectorStyle BackColor="#FFCC66" />
<OtherMonthDayStyle ForeColor="#CC9966" />
<NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
<DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" Font-Italic="True" />
<TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
<WeekendDayStyle BackColor="LightSalmon" />
</asp:Calendar>
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label><br />
选择的 月是:<asp:Label ID="Label2" runat="server"></asp:Label><br />
选择的 年是:<asp:Label ID="Label4" runat="server"></asp:Label><br />
<br />
显示输入的时间
<asp:DropDownList ID="ye" runat="server">
</asp:DropDownList>年<asp:DropDownList ID="mo" runat="server">
</asp:DropDownList>月<asp:DropDownList ID="da" runat="server">
</asp:DropDownList>日
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="切" Width="85px" /><br />
<br />
</div>
</form>
</body>
</html>
后台C#代码
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
{
string[][] arr; //声明一个数组来存储一年中的节日
protected void Page_Load(object sender, EventArgs e)
{
arr = new string[13][];//数组
for(int i=0;i<13;i++)
{
arr[i]=new string[32];
}
//下面是储存在数组中的节日
arr[1][1] = "圆蛋节";
arr[2][15]="没啥节样";
arr[3][8] = "妇女节";
arr[4][15] = "没啥节样";
arr[5][1] = "大假哦";
arr[6][1] = "娃娃节";
arr[7][1] = "jiandang节";
arr[7][15] = "洪川医药正式启用";
arr[8][1] = "兵仔儿节";
arr[9][10] = "孔子节";
arr[9][28] = "热烈庆祝,今天是川哥的生日哈!";
arr[10][1] = "大假哦";
arr[11][15] = "没啥节样";
arr[12][25] = "鬼节";
//填充年月日下拉菜单
for (int y = 1980; y < 2050; y++)
{//填充年下拉列表
ye.Items.Add(y.ToString());
}
for (int m = 1; m < 13; m++)
{//填充月下拉列表
mo.Items.Add(m.ToString());
}
for (int d = 1; d < 32; d++)
{//填充日下拉列表
da.Items.Add(d.ToString());
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (DropDownList1.SelectedValue)//循环判断
{
case "None"://注意是冒号
Calendar1.SelectionMode = CalendarSelectionMode.None;//设置选择方式为 没有
break;//跳出循环
case "DayWeekMonth":
Calendar1.SelectionMode = CalendarSelectionMode.DayWeekMonth;
break;
case "DayWeek":
Calendar1.SelectionMode = CalendarSelectionMode.DayWeek;
break;
case "Day":
Calendar1.SelectionMode = CalendarSelectionMode.Day;
break;
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{//当选择一个日期时激发
Label1.Text ="完整日期是 :"+ Calendar1.SelectedDate.ToShortDateString();
Label2.Text = Calendar1.SelectedDate.Month.ToString()+"月";
Label4.Text = Calendar1.SelectedDate.Year.ToString()+"年";
}
protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
{//当切换到其他月的时候激发
Label1.Text = "你真的要换到下个月??";
}
protected void Button1_Click(object sender, EventArgs e)
{//指定日期事件
string y = ye.SelectedValue.ToString();//得到年的值
string m = mo.SelectedValue.ToString();//得到月的值
string d = da.SelectedValue.ToString();//得到日的值
Calendar1.VisibleDate =Convert.ToDateTime(y+"-"+m+"-"+d);//指定被选中的日期
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)//注意这个事件名称是 DayRender
{
CalendarDay d = e.Day;//设置当前日期
TableCell c = e.Cell;//设置当前表格
if (d.IsOtherMonth)
{//如果是其他月份的话就清除本来的节日
c.Controls.Clear();
}
else
{
try
{
string txt = arr[d.Date.Month][d.Date.Day];//得到完整的当前[月][日]
if (txt != string.Empty)//看下里面是否有东西
{
c.Controls.Add(new LiteralControl("<br>"+txt));//如果有的话,就在日期数字后面换行显示出来
}
}
catch(Exception exe)
{
Response.Write(exe.ToString());
}
}
}
}
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
{
string[][] arr; //声明一个数组来存储一年中的节日
protected void Page_Load(object sender, EventArgs e)
{
arr = new string[13][];//数组
for(int i=0;i<13;i++)
{
arr[i]=new string[32];
}
//下面是储存在数组中的节日
arr[1][1] = "圆蛋节";
arr[2][15]="没啥节样";
arr[3][8] = "妇女节";
arr[4][15] = "没啥节样";
arr[5][1] = "大假哦";
arr[6][1] = "娃娃节";
arr[7][1] = "jiandang节";
arr[7][15] = "洪川医药正式启用";
arr[8][1] = "兵仔儿节";
arr[9][10] = "孔子节";
arr[9][28] = "热烈庆祝,今天是川哥的生日哈!";
arr[10][1] = "大假哦";
arr[11][15] = "没啥节样";
arr[12][25] = "鬼节";
//填充年月日下拉菜单
for (int y = 1980; y < 2050; y++)
{//填充年下拉列表
ye.Items.Add(y.ToString());
}
for (int m = 1; m < 13; m++)
{//填充月下拉列表
mo.Items.Add(m.ToString());
}
for (int d = 1; d < 32; d++)
{//填充日下拉列表
da.Items.Add(d.ToString());
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (DropDownList1.SelectedValue)//循环判断
{
case "None"://注意是冒号
Calendar1.SelectionMode = CalendarSelectionMode.None;//设置选择方式为 没有
break;//跳出循环
case "DayWeekMonth":
Calendar1.SelectionMode = CalendarSelectionMode.DayWeekMonth;
break;
case "DayWeek":
Calendar1.SelectionMode = CalendarSelectionMode.DayWeek;
break;
case "Day":
Calendar1.SelectionMode = CalendarSelectionMode.Day;
break;
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{//当选择一个日期时激发
Label1.Text ="完整日期是 :"+ Calendar1.SelectedDate.ToShortDateString();
Label2.Text = Calendar1.SelectedDate.Month.ToString()+"月";
Label4.Text = Calendar1.SelectedDate.Year.ToString()+"年";
}
protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
{//当切换到其他月的时候激发
Label1.Text = "你真的要换到下个月??";
}
protected void Button1_Click(object sender, EventArgs e)
{//指定日期事件
string y = ye.SelectedValue.ToString();//得到年的值
string m = mo.SelectedValue.ToString();//得到月的值
string d = da.SelectedValue.ToString();//得到日的值
Calendar1.VisibleDate =Convert.ToDateTime(y+"-"+m+"-"+d);//指定被选中的日期
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)//注意这个事件名称是 DayRender
{
CalendarDay d = e.Day;//设置当前日期
TableCell c = e.Cell;//设置当前表格
if (d.IsOtherMonth)
{//如果是其他月份的话就清除本来的节日
c.Controls.Clear();
}
else
{
try
{
string txt = arr[d.Date.Month][d.Date.Day];//得到完整的当前[月][日]
if (txt != string.Empty)//看下里面是否有东西
{
c.Controls.Add(new LiteralControl("<br>"+txt));//如果有的话,就在日期数字后面换行显示出来
}
}
catch(Exception exe)
{
Response.Write(exe.ToString());
}
}
}
}