本文实例讲述了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++; }
新闻热点
疑难解答
图片精选