This article mainly introduces the implementation of ASP. NET calendar based on calendar, involving the use of the Calendar control operation date and time-related arithmetic skills, the need for friends can refer to the following
The example in this article describes the blog-based calendar implementation of ASP. Share to everyone for your reference, as follows:
How to use the Calendar control of. NET to achieve the effect of the site calendar in the blog, we know that the most important function of the site calendar is that the day the blog owner wrote a log, click on the date, you will enter the selected date of the log list,
First of all We know that the server controls in. NET will be postback, and the first day of the calendar control will be postback when clicked. All we have to do is change the default link so that it doesn't trigger the postback event, and secondly, to know which day there is no log. As for there is no log, it is necessary to query the database.
There is a DayRender event in the calendar that is triggered every day, and we can start here by defining an array variable first:
Private int[] Arrcurrentdays, arrpredays, arrnextdays; Three variables are current month, former January, and next month private int intcurrentmonth, Intpremonth, Intnextmonth; Three integer array holds the date that the blog is written relative to the month
Then write the following code in the Calendar DayRender event:
CalendarDay d = ((Dayrendereventargs) e). Day; TableCell C = ((Dayrendereventargs) e). cell;//initializes the current month with the date array of the blog if (intpremonth = = 0) {intpremonth = D.date.month;//NOTE: The first month that we get when the Calendar control is initialized is not the current month, but the month of the first one months INTCU Rrentmonth = Intpremonth + 1; if (Intcurrentmonth >) intcurrentmonth = 1; Intnextmonth = Intcurrentmonth + 1; if (Intnextmonth >) intnextmonth = 1; Arrpredays = Getarrayday (D.date.year, intpremonth); Get an array of dates for the first one months of blogging arrcurrentdays = Getarrayday (D.date.year, intcurrentmonth);//Get the date array with the current month's blog Arrnextdays = Getarrayday (D.date.year, intnextmonth);//Get the date array with the blog next month}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= View "+d.dat e.day+ "Daily log >" + 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.D ay + "</a>")); } j + +; }
Here we note that the getArrayDay()
method is to query the database from the current month whether there is a log method, it returns an array, I wrote the following:
Public int[] Getarrayday (int intyear, int intmonth) { int[] Intarray = new int[31]; Select the matching record from the database, and save the date in the array string strSQL = "Select data from test where year (data) =" + Intyear + "and month (data) =" + Intmonth; Call the ExecuteReader method in the Dbhelperoledb custom class, which returns a OleDbDataReader type 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;}