Create a personal calendar control for your blog

xiaoxiao2021-03-06  99

The calendar control is one of the controls of the DOTNET, which is powerful, and it is useful in many project development, which is essential for the Blog system. It still needs to be carved in a good jade, in order to make it more beautiful and practical, we also need to develop it secondary.

The first step is to set up, this is required to make some adjustments to its related properties according to your needs. The picture below is the interface I adjusted.

The property is set as follows:

DayHeaderStyle-BackColor = "# 5e715e" OtherMonthDayStyle-ForeColor = "# cccccc" DayNameFormat = "Full" runat = "server" TitleStyle-ForeColor = "# ffffff" NextPrevStyle-ForeColor = "# ffffff" CellSpacing = "1" WeekendDayStyle-BackColor = "# eeeeee" DayHeaderStyle-ForeColor = "# ffffff" SelectionMode = "None" TodayDayStyle-BorderColor = "# 5e715e" TodayDayStyle-BorderWidth = "1" TodayDayStyle-Font-Bold = "true" TodayDayStyle-ForeColor = "# 5e715e" >

The second step is to adjust the internal function, this work is mainly concentrated on the processing of the following two events.

Prerender: When the server control will be present to the PAGE object included.

Dayrender: When the Calendar control is created every day in the control hierarchy.

First define three integer variables and integer arrays

Private int [] ArrcurrentDays, Arrpredays, ArrnextDays; // The three variables are the current month, January, and the next month private int int, intnextMonth, IntnextMonth; // three integer array stores relative months written with blog Date Protected System.Web.ui.WebControls.calendar Calendar1; // This is our calendar control.

2. Below I will give the source code of these two events, and explain the features it implemented below. If you don't understand, you can first look at the following description.

Prerender

private void Calendar1_PreRender (object sender, System.EventArgs e) {Thread threadCurrent = Thread.CurrentThread; CultureInfo ciNew = (CultureInfo) threadCurrent.CurrentCulture.Clone (); ciNew.DateTimeFormat.DayNames = new string [] { "day", " A "," two "," three "," four "," five "," six "}; cinew.datetimeformat.firstdayofweek = dayofweek.sunday; threadcurrent.currentculture = cinew;}

The above code changes the display of the weeks. You can change your name to display only if you want to change the value of the character array. Dayrender

Private void Calendar1_dayrender (object sender, system.web.ui.webcontrols.dayrenderEventArgs e) {// This control occurs every day. Calendarday D = ((DayRendeventargs) e) .day; TableCell C = ((DayRendreventArgs) e). Cell;

// Initialize the current month's date array if (INTPREMONTH == 0) {INTPREMONTH = D.date.month; // Note: The first month we get in the first month when the calendar control is initialized, but the former one intCurrentMonth month month = intPreMonth 1; if (intCurrentMonth> 12) intCurrentMonth = 1; intNextMonth = intCurrentMonth 1; if (intNextMonth> 12) intNextMonth = 1; arrPreDays = getArrayDay (d.Date.Year, intPreMonth); // Received the date array of blogs from the previous month ArrcurrentDays = GetArrayDay (D.date.Year, INTCURRENTMONTH); // Get date array of blogs in the month arrnextDays = GetArrayDay (D.date.Year, IntnexTmonth); // Get next month There is a date array of 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 LitralControl (" D.date.Day "));} J ;}} else if (d.date.month. Equals (intcurrentmonth)) {while (! Arrcurrentdays [j] .equals (0)) {i f (D.Date.Day.equals (ArrcurrentDays [J])) {c.controls.clear (); c.controls.add (new literalcontrol (" d.date.day ");} 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 LitralControl (" D.date.day "));} J ;}}}

Date Control A page can display a date of three months, the current month is complete, some date and next date. DayRender event initializes the display method of the specific date, here we have to add a hyperlink on the date with Blog content. So we need to get three arrays during initialization, and store the date of writing with Blog for three consecutive months, respectively. Then compare with the current date in turn, add a link. When using a DayRender event, you must do not forget that it is performed once at each date initialization, which means that the event initialization of the calendar control is to execute 42 times, so do not add as possible, don't add Judging the repeated database operation, I didn't pay attention when I started, and two read library statements were written in the event, and the results were seriously affected.

The following method is that I have used to get a date array.

// Get the date array of Blog this month private int [] GetArrayDay (int tent "{int [] INTARRAY = new int [31]; // Select records in accordance with the requirements in the database, deposit the date into array string strSql = "select content_time from content where year (content_time) =" intYear "and month (content_time) =" intMonth; dr = SqlHandle.GetDr (strSql); while (dr.Read ()) {if (i = = 0) {INTARRAY [I] = Dr.getdateTime (0) .day; i ;} else f (Dr.GetdateTime (0) .day! = INTARRAY [I-1]) {Intarray [i] = Dr.GetDatetime (0) .day; i ;}} Dr.close (); Return INTARRAY

转载请注明原文地址:https://www.9cbs.com/read-126080.html

New Post(0)