今天在网上看到实现博客日志的日历的代码,把代码复制到自己的机器上运行,并在数据中插入几个数据,发现有个bug,修改一下下 ,小女子才学尚浅,如有错误,请大家不要见笑!
一、页面代码calTest.aspx
<%
...
@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxCal.aspx.cs" Inherits="test_AjaxCal"
%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title > WebForm1 </ title >
< meta name ="GENERATOR" Content ="Microsoft Visual Studio .NET 7.1" >
< meta name ="CODE_LANGUAGE" Content ="C#" >
< meta name ="vs_defaultClientScript" content ="JavaScript" >
< meta name ="vs_targetSchema" content ="http://schemas.microsoft.com/intellisense/ie5" >
</ HEAD >
< body MS_POSITIONING ="GridLayout" >
< form id ="Form1" method ="post" runat ="server" >
< asp:calendar id ="Calendar1" Width ="350px" TitleStyle-BackColor ="#000000" BorderColor ="White"
DayHeaderStyle-BackColor ="#5e715e" OtherMonthDayStyle-ForeColor ="#cccccc"
runat ="server" TitleStyle-ForeColor ="#ffffff" NextPrevStyle-ForeColor ="#ffffff"
WeekendDayStyle-BackColor ="#eeeeee" DayHeaderStyle-ForeColor ="#ffffff" SelectionMode ="None"
TodayDayStyle-BorderColor ="#5e715e" TodayDayStyle-BorderWidth ="1" TodayDayStyle-Font-Bold ="true"
TodayDayStyle-ForeColor ="#5e715e" BackColor ="White" Font-Names ="Verdana" Font-Size ="9pt" ForeColor ="Black" Height ="190px" BorderWidth ="1px" NextPrevFormat ="FullMonth" >
< TodayDayStyle BorderWidth ="1px" BorderColor ="#5E715E" BackColor ="#CCCCCC" ></ TodayDayStyle >
< NextPrevStyle VerticalAlign ="Bottom" Font-Bold ="True" Font-Size ="8pt" ForeColor ="#333333" ></ NextPrevStyle >
< DayHeaderStyle Font-Bold ="True" Font-Size ="8pt" ></ DayHeaderStyle >
< TitleStyle BackColor ="White" BorderColor ="Black" Font-Bold ="True" BorderWidth ="4px" Font-Size ="12pt" ForeColor ="#333399" ></ TitleStyle >
< OtherMonthDayStyle ForeColor ="#999999" ></ OtherMonthDayStyle >
< SelectedDayStyle BackColor ="#333399" ForeColor ="White" />
</ asp:calendar >
< asp:TextBox ID ="TextBox1" runat ="server" ></ asp:TextBox >
</ form >
</ body >
</ HTML >
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title > WebForm1 </ title >
< meta name ="GENERATOR" Content ="Microsoft Visual Studio .NET 7.1" >
< meta name ="CODE_LANGUAGE" Content ="C#" >
< meta name ="vs_defaultClientScript" content ="JavaScript" >
< meta name ="vs_targetSchema" content ="http://schemas.microsoft.com/intellisense/ie5" >
</ HEAD >
< body MS_POSITIONING ="GridLayout" >
< form id ="Form1" method ="post" runat ="server" >
< asp:calendar id ="Calendar1" Width ="350px" TitleStyle-BackColor ="#000000" BorderColor ="White"
DayHeaderStyle-BackColor ="#5e715e" OtherMonthDayStyle-ForeColor ="#cccccc"
runat ="server" TitleStyle-ForeColor ="#ffffff" NextPrevStyle-ForeColor ="#ffffff"
WeekendDayStyle-BackColor ="#eeeeee" DayHeaderStyle-ForeColor ="#ffffff" SelectionMode ="None"
TodayDayStyle-BorderColor ="#5e715e" TodayDayStyle-BorderWidth ="1" TodayDayStyle-Font-Bold ="true"
TodayDayStyle-ForeColor ="#5e715e" BackColor ="White" Font-Names ="Verdana" Font-Size ="9pt" ForeColor ="Black" Height ="190px" BorderWidth ="1px" NextPrevFormat ="FullMonth" >
< TodayDayStyle BorderWidth ="1px" BorderColor ="#5E715E" BackColor ="#CCCCCC" ></ TodayDayStyle >
< NextPrevStyle VerticalAlign ="Bottom" Font-Bold ="True" Font-Size ="8pt" ForeColor ="#333333" ></ NextPrevStyle >
< DayHeaderStyle Font-Bold ="True" Font-Size ="8pt" ></ DayHeaderStyle >
< TitleStyle BackColor ="White" BorderColor ="Black" Font-Bold ="True" BorderWidth ="4px" Font-Size ="12pt" ForeColor ="#333399" ></ TitleStyle >
< OtherMonthDayStyle ForeColor ="#999999" ></ OtherMonthDayStyle >
< SelectedDayStyle BackColor ="#333399" ForeColor ="White" />
</ asp:calendar >
< asp:TextBox ID ="TextBox1" 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;
using System.Threading;
using System.Globalization;
using System.Data.SqlClient;
public partial class test_AjaxCal : System.Web.UI.Page
... {
// protected System.Web.UI.WebControls.Calendar Calendar;
private int[] arrCurrentDays, arrPreDays, arrNextDays; //三个变量分别是当前月,前一月,和下一个月
private int intCurrentMonth, intPreMonth, intNextMonth; //三个整型数组存放相对月份写有blog的日期
private int intCurrentYear, intPreYear, intNextYear; //三个整型数组存放相对年份写有blog的日期(次处是我添加上来的)
protected void Page_Load(object sender, EventArgs e)
...{
TextBox1.Text = "reaer";
}
private void Calendar1_PreRender(object sender, System.EventArgs e)
...{
Thread threadCurrent = Thread.CurrentThread;
CultureInfo ciNew = (CultureInfo)threadCurrent.CurrentCulture.Clone();
ciNew.DateTimeFormat.DayNames = new string[]...{"日","一","二","三","四","五","六"};
ciNew.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Sunday;
threadCurrent.CurrentCulture = ciNew;
}
private void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
...{
//该控件在创建每一天时发生。
CalendarDay d = ((DayRenderEventArgs)e).Day;
TableCell c = ((DayRenderEventArgs)e).Cell;
// 初始化当前月有Blog的日期数组
if (intPreMonth == 0)
...{
//复制当前的年份 前一年 后一年
intCurrentYear = d.Date.Year;
intPreYear = intCurrentYear;
intNextYear = intCurrentYear;
intPreMonth = d.Date.Month; // 注意:日历控件初始化时我们得到的第一个月并不是当前月,而是前一个月的月份
intCurrentMonth = intPreMonth + 1;
if (intCurrentMonth > 12)
...{
intCurrentMonth = 1;
intCurrentYear = intCurrentYear + 1;
}
intNextMonth = intCurrentMonth + 1;
if (intNextMonth > 12)
...{
d.Date.AddYears(1);
intNextYear = intNextYear +1;
intNextMonth = 1;
}
arrPreDays = getArrayDay(intPreYear, intPreMonth); //得到前一个月有blog的日期数组
arrCurrentDays = getArrayDay(intCurrentYear, intCurrentMonth);//得到当月有blog的日期数组
arrNextDays = getArrayDay(intNextYear, intNextMonth);//得到下个月有blog的日期数组
}
int j=0;
if( d.Date.Month.Equals(intPreMonth) )
...{
while( ! arrPreDays[j].Equals(0) )
...{
if(d.Date.Day.Equals(arrPreDays[j]))
...{
c.Controls.Clear();
c.Controls.Add(new LiteralControl("<a href=WebForm1.aspx?year="+d.Date.Year+"-"+
d.Date.Month+"-"+d.Date.Day+">"+d.Date.Day+"</a>"));
}
j++;
}
}
else if( d.Date.Month.Equals(intCurrentMonth) )
...{
while( !arrCurrentDays[j].Equals(0) )
...{
if(d.Date.Day.Equals(arrCurrentDays[j]))
...{
c.Controls.Clear();
c.Controls.Add(new LiteralControl("<a href=WebForm1.aspx?time="+d.Date.Year+"-"+
d.Date.Month+"-"+d.Date.Day+" title='"+d.Date.Year+'/'+d.Date.Month+'/'+d.Date.Day+"'>"+d.Date.Day+"</a>"));
}
j++;
}
}
else if( d.Date.Month.Equals(intNextMonth) )
...{
while( ! arrNextDays[j].Equals(0) )
...{
if(d.Date.Day.Equals(arrNextDays[j]))
...{
c.Controls.Clear();
c.Controls.Add(new LiteralControl("<a href=WebForm1.aspx?year="+d.Date.Year+"-"+
d.Date.Month+"-"+d.Date.Day+">"+d.Date.Day+"</a>"));
}
j++;
}
}
}
private int[] getArrayDay(int intYear,int intMonth)
...{
int i=0;
int[] intArray = new int[31];
//从数据库里选取符合要求的记录,将日期存入数组
string mySelectQuery = "select OrderDate from test where year(OrderDate)="+intYear+
" and month(OrderDate)="+intMonth;
//SqlConnection myConnection=new SqlConnection("server=localhost;UID=sa;pwd=fan;database=test");
//SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
//myConnection.Open();
//SqlDataReader myReader;
//myReader = myCommand.ExecuteReader();
MySqlDB mdb=new MySqlDB(mySelectQuery);
while (!mdb.EOF)
...{
if( i==0 )
...{
intArray[i] =mdb["OrderDate"].ToDateTime().Day;
i++;
}
else if (mdb["OrderDate"].ToDateTime().Day != intArray[i - 1])
...{
intArray[i] = mdb["OrderDate"].ToDateTime().Day;
i++;
}
mdb.MoveNext();
}
return intArray;
}
网页 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.Calendar1.DayRender += new System.Web.UI.WebControls.DayRenderEventHandler(this.Calendar1_DayRender);
this.Calendar1.PreRender += new System.EventHandler(this.Calendar1_PreRender);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
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;
using System.Threading;
using System.Globalization;
using System.Data.SqlClient;
public partial class test_AjaxCal : System.Web.UI.Page
... {
// protected System.Web.UI.WebControls.Calendar Calendar;
private int[] arrCurrentDays, arrPreDays, arrNextDays; //三个变量分别是当前月,前一月,和下一个月
private int intCurrentMonth, intPreMonth, intNextMonth; //三个整型数组存放相对月份写有blog的日期
private int intCurrentYear, intPreYear, intNextYear; //三个整型数组存放相对年份写有blog的日期(次处是我添加上来的)
protected void Page_Load(object sender, EventArgs e)
...{
TextBox1.Text = "reaer";
}
private void Calendar1_PreRender(object sender, System.EventArgs e)
...{
Thread threadCurrent = Thread.CurrentThread;
CultureInfo ciNew = (CultureInfo)threadCurrent.CurrentCulture.Clone();
ciNew.DateTimeFormat.DayNames = new string[]...{"日","一","二","三","四","五","六"};
ciNew.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Sunday;
threadCurrent.CurrentCulture = ciNew;
}
private void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
...{
//该控件在创建每一天时发生。
CalendarDay d = ((DayRenderEventArgs)e).Day;
TableCell c = ((DayRenderEventArgs)e).Cell;
// 初始化当前月有Blog的日期数组
if (intPreMonth == 0)
...{
//复制当前的年份 前一年 后一年
intCurrentYear = d.Date.Year;
intPreYear = intCurrentYear;
intNextYear = intCurrentYear;
intPreMonth = d.Date.Month; // 注意:日历控件初始化时我们得到的第一个月并不是当前月,而是前一个月的月份
intCurrentMonth = intPreMonth + 1;
if (intCurrentMonth > 12)
...{
intCurrentMonth = 1;
intCurrentYear = intCurrentYear + 1;
}
intNextMonth = intCurrentMonth + 1;
if (intNextMonth > 12)
...{
d.Date.AddYears(1);
intNextYear = intNextYear +1;
intNextMonth = 1;
}
arrPreDays = getArrayDay(intPreYear, intPreMonth); //得到前一个月有blog的日期数组
arrCurrentDays = getArrayDay(intCurrentYear, intCurrentMonth);//得到当月有blog的日期数组
arrNextDays = getArrayDay(intNextYear, intNextMonth);//得到下个月有blog的日期数组
}
int j=0;
if( d.Date.Month.Equals(intPreMonth) )
...{
while( ! arrPreDays[j].Equals(0) )
...{
if(d.Date.Day.Equals(arrPreDays[j]))
...{
c.Controls.Clear();
c.Controls.Add(new LiteralControl("<a href=WebForm1.aspx?year="+d.Date.Year+"-"+
d.Date.Month+"-"+d.Date.Day+">"+d.Date.Day+"</a>"));
}
j++;
}
}
else if( d.Date.Month.Equals(intCurrentMonth) )
...{
while( !arrCurrentDays[j].Equals(0) )
...{
if(d.Date.Day.Equals(arrCurrentDays[j]))
...{
c.Controls.Clear();
c.Controls.Add(new LiteralControl("<a href=WebForm1.aspx?time="+d.Date.Year+"-"+
d.Date.Month+"-"+d.Date.Day+" title='"+d.Date.Year+'/'+d.Date.Month+'/'+d.Date.Day+"'>"+d.Date.Day+"</a>"));
}
j++;
}
}
else if( d.Date.Month.Equals(intNextMonth) )
...{
while( ! arrNextDays[j].Equals(0) )
...{
if(d.Date.Day.Equals(arrNextDays[j]))
...{
c.Controls.Clear();
c.Controls.Add(new LiteralControl("<a href=WebForm1.aspx?year="+d.Date.Year+"-"+
d.Date.Month+"-"+d.Date.Day+">"+d.Date.Day+"</a>"));
}
j++;
}
}
}
private int[] getArrayDay(int intYear,int intMonth)
...{
int i=0;
int[] intArray = new int[31];
//从数据库里选取符合要求的记录,将日期存入数组
string mySelectQuery = "select OrderDate from test where year(OrderDate)="+intYear+
" and month(OrderDate)="+intMonth;
//SqlConnection myConnection=new SqlConnection("server=localhost;UID=sa;pwd=fan;database=test");
//SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
//myConnection.Open();
//SqlDataReader myReader;
//myReader = myCommand.ExecuteReader();
MySqlDB mdb=new MySqlDB(mySelectQuery);
while (!mdb.EOF)
...{
if( i==0 )
...{
intArray[i] =mdb["OrderDate"].ToDateTime().Day;
i++;
}
else if (mdb["OrderDate"].ToDateTime().Day != intArray[i - 1])
...{
intArray[i] = mdb["OrderDate"].ToDateTime().Day;
i++;
}
mdb.MoveNext();
}
return intArray;
}
网页 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.Calendar1.DayRender += new System.Web.UI.WebControls.DayRenderEventHandler(this.Calendar1_DayRender);
this.Calendar1.PreRender += new System.EventHandler(this.Calendar1_PreRender);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
三、大功告成!就可以看到漂亮的日历了
另外还有数据库不要忘记了,在数据中插入几条数据,就可以看到效果了
DROP
TABLE
IF
EXISTS
`test`.`test`;
CREATE TABLE `test`.`test` (
`OrderDate` datetime NOT NULL ,
PRIMARY KEY (`OrderDate`)
) ENGINE = InnoDB DEFAULT CHARSET = latin1;
CREATE TABLE `test`.`test` (
`OrderDate` datetime NOT NULL ,
PRIMARY KEY (`OrderDate`)
) ENGINE = InnoDB DEFAULT CHARSET = latin1;
哈哈~~~如有错误,还请大侠多多指教~~~~~~~~~~~~~~~~~~~~~~~