显示日程的自定日历
效果图如下:
示例代码如下:
--------------
HTML部分
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="PrivateWork_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">
<table>
<tr>
<td colspan="7" align="center">日历</td>
</tr>
<tr>
<td colspan="7">
<asp:Button ID="btn_PreYear" runat="server" Text="<<" OnClick="btn_PreYear_Click" />
<asp:Button ID="btn_PreMonth" runat="server" Text="<" OnClick="btn_PreMonth_Click" />
<asp:DropDownList ID="ddl_Year" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl_Year_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="ddl_Month" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl_Month_SelectedIndexChanged">
<asp:ListItem Text="1月" Value="1"></asp:ListItem>
<asp:ListItem Text="2月" Value="2"></asp:ListItem>
<asp:ListItem Text="3月" Value="3"></asp:ListItem>
<asp:ListItem Text="4月" Value="4"></asp:ListItem>
<asp:ListItem Text="5月" Value="5"></asp:ListItem>
<asp:ListItem Text="6月" Value="6"></asp:ListItem>
<asp:ListItem Text="7月" Value="7"></asp:ListItem>
<asp:ListItem Text="8月" Value="8"></asp:ListItem>
<asp:ListItem Text="9月" Value="9"></asp:ListItem>
<asp:ListItem Text="10月" Value="10"></asp:ListItem>
<asp:ListItem Text="11月" Value="11"></asp:ListItem>
<asp:ListItem Text="12月" Value="12"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btn_NextMonth" runat="server" Text=">" OnClick="btn_NextMonth_Click" />
<asp:Button ID="btn_NextYear" runat="server" Text=">>" OnClick="btn_NextYear_Click" />
</td>
</tr>
<tr>
<td>日</td>
<td>一</td>
<td>二</td>
<td>三</td>
<td>四</td>
<td>五</td>
<td>六</td>
</tr>
<tr>
<td runat="server" id="d1"></td>
<td runat="server" id="d2"></td>
<td runat="server" id="d3"></td>
<td runat="server" id="d4"></td>
<td runat="server" id="d5"></td>
<td runat="server" id="d6"></td>
<td runat="server" id="d7"></td>
</tr>
<tr>
<td runat="server" id="d8"></td>
<td runat="server" id="d9"></td>
<td runat="server" id="d10"></td>
<td runat="server" id="d11"></td>
<td runat="server" id="d12"></td>
<td runat="server" id="d13"></td>
<td runat="server" id="d14"></td>
</tr>
<tr>
<td runat="server" id="d15"></td>
<td runat="server" id="d16"></td>
<td runat="server" id="d17"></td>
<td runat="server" id="d18"></td>
<td runat="server" id="d19"></td>
<td runat="server" id="d20"></td>
<td runat="server" id="d21"></td>
</tr>
<tr>
<td runat="server" id="d22"></td>
<td runat="server" id="d23"></td>
<td runat="server" id="d24"></td>
<td runat="server" id="d25"></td>
<td runat="server" id="d26"></td>
<td runat="server" id="d27"></td>
<td runat="server" id="d28"></td>
</tr>
<tr>
<td runat="server" id="d29"></td>
<td runat="server" id="d30"></td>
<td runat="server" id="d31"></td>
<td runat="server" id="d32"></td>
<td runat="server" id="d33"></td>
<td runat="server" id="d34"></td>
<td runat="server" id="d35"></td>
</tr>
<tr>
<td runat="server" id="d36"></td>
<td runat="server" id="d37"></td>
<td runat="server" id="d38"></td>
<td runat="server" id="d39"></td>
<td runat="server" id="d40"></td>
<td runat="server" id="d41"></td>
<td runat="server" id="d42"></td>
</tr>
<tr>
<td bgcolor="#EBF7EA"></td>
<td colspan="6" >
某日有事件</td>
</tr>
<tr>
<td bgcolor="#FFEEFF"></td>
<td colspan="6" >
今天</td>
</tr>
<tr>
<td bgcolor="#003366"></td>
<td colspan="6" >
查看的日期</td>
</tr>
</table>
<br />
<br />
当前所点击的日期<asp:TextBox ID="txt_NowClickDate" runat="server"></asp:TextBox>
</form>
</body>
</html>
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 PrivateWork_Default : System.Web.UI.Page
{
Page_Load#region Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fn_SetYear();
if (!string.IsNullOrEmpty(Request["Year"]))
{
this.ddl_Year.SelectedValue = Request["Year"];
this.ddl_Month.SelectedValue = Request["Month"];
}
fn_SetCal();
}
}
#endregion
页面事件#region 页面事件
前一年#region 前一年
protected void btn_PreYear_Click(object sender, EventArgs e)
{
int iIndex = this.ddl_Year.SelectedIndex;
if ( iIndex!= 0)
{
this.ddl_Year.SelectedIndex = iIndex - 1;
fn_SetCal();
}
}
#endregion
前一月#region 前一月
protected void btn_PreMonth_Click(object sender, EventArgs e)
{
int iIndex = this.ddl_Month.SelectedIndex;
if (iIndex != 0)
{
this.ddl_Month.SelectedIndex = iIndex - 1;
fn_SetCal();
}
}
#endregion
后一月#region 后一月
protected void btn_NextMonth_Click(object sender, EventArgs e)
{
int iIndex = this.ddl_Month.SelectedIndex;
if (iIndex != 11)
{
this.ddl_Month.SelectedIndex = iIndex + 1;
fn_SetCal();
}
}
#endregion
后一年#region 后一年
protected void btn_NextYear_Click(object sender, EventArgs e)
{
int iIndex = this.ddl_Year.SelectedIndex;
if (iIndex != 10)
{
this.ddl_Year.SelectedIndex = iIndex + 1;
fn_SetCal();
}
}
#endregion
年 DropDownList 选择 改变#region 年 DropDownList 选择 改变
protected void ddl_Year_SelectedIndexChanged(object sender, EventArgs e)
{
fn_SetCal();
}
#endregion
月 DropDownList 选择 改变#region 月 DropDownList 选择 改变
protected void ddl_Month_SelectedIndexChanged(object sender, EventArgs e)
{
fn_SetCal();
}
#endregion
#endregion
自定义方法#region 自定义方法
根据日期 设置年份#region 根据日期 设置年份
private void fn_SetYear()
{
int iYear = DateTime.Today.Year;
//前五年
for (int i = 5; i > 0; i--)
{
int tmpYear = iYear - i;
this.ddl_Year.Items.Add(new ListItem(tmpYear.ToString()+"年",tmpYear.ToString()));
}
//当前年
this.ddl_Year.Items.Add(new ListItem(iYear.ToString() + "年", iYear.ToString()));
//后五年
for (int j = 1; j < 6; j++)
{
int tmpYear = iYear + j;
this.ddl_Year.Items.Add(new ListItem(tmpYear.ToString() + "年", tmpYear.ToString()));
}
//
this.ddl_Year.SelectedValue = iYear.ToString();
this.ddl_Month.SelectedValue = DateTime.Today.Month.ToString();
this.txt_NowClickDate.Text = DateTime.Today.ToShortDateString();
}
#endregion
根据日期 设置当前的日历#region 根据日期 设置当前的日历
private void fn_SetCal()
{
int iYear = Convert.ToInt32(this.ddl_Year.SelectedValue);//年
int iMonth = Convert.ToInt32(this.ddl_Month.SelectedValue);//月
DateTime dStart = new DateTime(iYear,iMonth,1);//该年月的第一天
int iDays = DateTime.DaysInMonth(iYear,iMonth);//该年月的总共天数
int iStartDay = (Convert.ToInt32(dStart.DayOfWeek) + 1);//第一天为周几
//当前选中日期
int iSelectDay = 0;
if(string.IsNullOrEmpty(Request["Day"]))
{
iSelectDay = DateTime.Today.Day;
}
else
{
iSelectDay = Convert.ToInt32(Request["Day"]);
}
//日历绘制
for (int i = 1; i < 43; i++)
{
HtmlTableCell tmpCell = (HtmlTableCell)FindControl("d" + i);
tmpCell.BgColor = "";
if ((i < iStartDay) || ((i - iStartDay + 1) > iDays))
{
tmpCell.InnerText = "";
}
else
{
//手形
tmpCell.Style.Add("cursor","hand");
string strOnClickJS = "window.location='Default.aspx?Year=" + iYear.ToString();
strOnClickJS += "&Month=" + iMonth.ToString();
strOnClickJS += "&Day=" + Convert.ToString(i - iStartDay + 1) + "' ";
tmpCell.Attributes.Add("onclick", strOnClickJS);//页面回发 以便到更多服务器端自定义处理
//得到当前绘制的日期
DateTime currDrawDate = new DateTime(iYear, iMonth, (i - iStartDay + 1));
//得到当前绘制的日期的事件数
int iCount = fn_GetEventCount(currDrawDate);
if (DateTime.Equals(currDrawDate, DateTime.Today))//等于今天
{
tmpCell.InnerText = Convert.ToString((i - iStartDay + 1));
tmpCell.BgColor = "#FFEEFF";
if (iCount != 0)//有日志
{
tmpCell.Attributes.Add("title", "有" + iCount.ToString() + "个事件");
}
}
else//不是今天
{
tmpCell.InnerText = Convert.ToString((i - iStartDay + 1));
if (iCount != 0)//有日志
{
tmpCell.Attributes.Add("title", "有" + iCount.ToString() + "个事件");
tmpCell.BgColor = "#EBF7EA";
}
}
}
//当前查看日期
if ((i - iStartDay + 1) == iSelectDay)
{
tmpCell.BgColor = "#003366";
this.txt_NowClickDate.Text = this.ddl_Year.SelectedValue + "-" + this.ddl_Month.SelectedValue + "-" + iSelectDay.ToString();
}
}
}
#endregion
得到当天事件日志的笔数#region 得到当天事件日志的笔数
private int fn_GetEventCount(DateTime dDate)
{
int iCount = 0;
string strSql = " SELECT COUNT(EventSeqID) FROM CalEvent ";
strSql += " WHERE EventStartDate<='"+dDate.ToShortDateString()+"' ";
strSql += " AND EventEndDate>='" + dDate.ToShortDateString() + "' ";
DBClass db = new DBClass();
iCount = Convert.ToInt32(db.RunSelectGetScalar(strSql));
return iCount;
}
#endregion
#endregion
}
posted on 2007-08-16 10:48 freeliver54 阅读(823) 评论(0) 编辑 收藏 举报