这期内容当中小编将会给大家带来有关如何在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。