本文實(shí)例講述了asp.net基于Calendar實(shí)現(xiàn)blog日歷功能。,具體如下:
怎樣用.net的Calendar控件來實(shí)現(xiàn)blog中站點(diǎn)日歷的效果呢,我們知道站點(diǎn)日歷最重要的功能就是,顯現(xiàn)在哪天blog主人寫了日志,點(diǎn)擊日期,你將進(jìn)入所選日期的日志列表,
首先,我們知道.net中的服務(wù)器控件是會(huì)進(jìn)行Postback的,Calendar控件中的第一天在點(diǎn)擊時(shí),就會(huì)進(jìn)行一次postback我們要做的就是改變它默認(rèn)的鏈接,使它不觸發(fā)postback事件,其次,就是要知道哪一天有沒有日志。至于有沒有日志,就要去數(shù)據(jù)庫查詢了。
在Calendar中有一個(gè)DayRender事件,該事件在呈現(xiàn)每一天時(shí)觸發(fā),我們可以從這里入手,首先定義一個(gè)數(shù)組變量:
private int[] arrCurrentDays, arrPreDays, arrNextDays; //三個(gè)變量分別是當(dāng)前月,前一月,和下一個(gè)月private int intCurrentMonth, intPreMonth, intNextMonth; //三個(gè)整型數(shù)組存放相對(duì)月份寫有blog的日期
然后在Calendar的DayRender事件中寫下如下代碼:
CalendarDay d = ((DayRenderEventArgs)e).Day;TableCell c = ((DayRenderEventArgs)e).Cell;// 初始化當(dāng)前月有Blog的日期數(shù)組if (intPreMonth == 0){ intPreMonth = d.Date.Month; // 注意:日歷控件初始化時(shí)我們得到的第一個(gè)月并不是當(dāng)前月,而是前一個(gè)月的月份 intCurrentMonth = intPreMonth + 1; if (intCurrentMonth > 12) intCurrentMonth = 1; intNextMonth = intCurrentMonth + 1; if (intNextMonth > 12) intNextMonth = 1; arrPreDays = getArrayDay(d.Date.Year, intPreMonth); //得到前一個(gè)月有blog的日期數(shù)組 arrCurrentDays = getArrayDay(d.Date.Year, intCurrentMonth);//得到當(dāng)月有blog的日期數(shù)組 arrNextDays = getArrayDay(d.Date.Year, intNextMonth);//得到下個(gè)月有blog的日期數(shù)組}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++; }
新聞熱點(diǎn)
疑難解答
圖片精選