本文实例报告了asp.net基于Calendar完成blog日历功用。分享给人人供人人参考,细致以下:
怎样用.net的Calendar控件来完成blog中站点日历的结果呢,我们晓得站点日历最主要的功用就是,展现在哪天blog主人写了日记,点击日期,你将进入所选日期的日记列表,
起首,我们晓得.net中的服务器控件是会举行Postback的,Calendar控件中的第一天在点击时,就会举行一次postback我们要做的就是转变它默许的链接,使它不触发postback事宜,其次,就是要晓得哪一天有无日记。至于有无日记,就要去数据库查询了。
在Calendar中有一个DayRender事宜,该事宜在显现每一天时触发,我们能够从这里入手,起首定义一个数组变量:
private int[] arrCurrentDays, arrPreDays, arrNextDays; //三个变量分别是当前月,前一月,和下一个月 private int intCurrentMonth, intPreMonth, intNextMonth; //三个整型数组寄存相对月份写有blog的日期
然后在Calendar的DayRender事宜中写下以下代码:
CalendarDay d = ((DayRenderEventArgs)e).Day; TableCell c = ((DayRenderEventArgs)e).Cell; // 初始化当前月有Blog的日期数组 if (intPreMonth == 0) { intPreMonth = d.Date.Month; // 注重:日历控件初始化时我们获得的第一个月并非当前月,而是前一个月的月份 intCurrentMonth = intPreMonth + 1; if (intCurrentMonth > 12) intCurrentMonth = 1; intNextMonth = intCurrentMonth + 1; if (intNextMonth > 12) intNextMonth = 1; arrPreDays = getArrayDay(d.Date.Year, intPreMonth); //获得前一个月有blog的日期数组 arrCurrentDays = getArrayDay(d.Date.Year, intCurrentMonth);//获得当月有blog的日期数组 arrNextDays = getArrayDay(d.Date.Year, 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="day.aspx?year=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" mce_href="day.aspx?year=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" " + d.Date.Year + "&month=" + d.Date.Month + "&day=" + 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="day.aspx?year=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" mce_href="day.aspx?year=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" " + d.Date.Year + "&month=" + d.Date.Month + "&day=" + d.Date.Day + " title=检察"+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="day.aspx?year=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" mce_href="day.aspx?year=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" " + d.Date.Year + "&month=" + d.Date.Month + "&day=" + d.Date.Day + ">" + d.Date.Day + "</a>")); } j++; }
在这里我们注重的是getArrayDay()
要领是从数据库里查询当月是不是有日记的要领,它返回的是一个数组,我写的内容以下:
public int[] getArrayDay(int intYear, int intMonth) { int[] intArray = new int[31]; //从数据库里拔取符合要求的纪录,将日期存入数组 string strSql = "select data from test where year(data)=" + intYear + " and month(data)=" + intMonth; //挪用DbHelperOleDb自定义类中的ExecuteReader要领,它返回的是一个OleDbDataReader型 OleDbDataReader dr = dbAccess.DbHelperOleDb.ExecuteReader(strSql); int i = 0; while (dr.Read()) { if (i == 0) { intArray[i] = Convert.ToDateTime(dr["data"].ToString()).Day; string a=Convert.ToString(intArray[i]); i++; } else if (Convert.ToDateTime(dr["data"].ToString()).Day != intArray[i - 1]) { intArray[i] = Convert.ToDateTime(dr["data"].ToString()).Day; i++; } } return intArray; }
以上就是基于Calendar完成blog日历的实例详解的细致内容,更多请关注ki4网别的相干文章!