首页 > 编程 > .NET > 正文

asp.net基于Calendar实现blog日历功能示例

2024-07-10 13:32:31
字体:
来源:转载
供稿:网友

本文实例讲述了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 + "    }    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;}

ok ,这样就行了,我们运行一下就可以看到效果了..

希望本文所述对大家asp.net程序设计有所帮助。


注:相关教程知识阅读请移步到ASP.NET教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表