這篇文章主要介紹了asp.net基於Calendar實作blog日曆功能,涉及asp.net使用Calendar控制項操作日期與時間相關運算技巧,需要的朋友可以參考下方
本文實例講述了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日曆的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!