基於Calendar實現blog日曆的執行個體詳解

來源:互聯網
上載者:User
這篇文章主要介紹了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;}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.