您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關如何在asp.net中使用Calendar實現一個blog日歷功能,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
首先定義一個數組變量:
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; }
上述就是小編為大家分享的如何在asp.net中使用Calendar實現一個blog日歷功能了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。