Create a personalized calendar control for your blog

Source: Internet
Author: User
Tags arrays integer thread
Control | The Calendar control is one of the dotnet controls that is powerful and useful in many project development, and is essential for blogging systems. The good jade also needs to carve, in order to make it more beautiful and practical, we also need to develop it two times.

The first step is the appearance setting, which, depending on your needs, can only be adjusted for its related properties. The following figure is my adjusted interface



The properties are set as follows: <asp:calendar id= "Calendar1" cellpadding= "2" width= "160px" titlestyle-backcolor= "#000000" bordercolor= "# AAAAAA "
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, which focuses on the following two events.

PreRender: Occurs when a server control is to be rendered to its contained page object.

DayRender: Occurs when you create each day for a calendar control in the control hierarchy.

Define three integer variables and integer arrays first
Private int[] arrcurrentdays,arrpredays,arrnextdays; Three variables are the current month, the previous January, and the next one months
private int intcurrentmonth,intpremonth,intnextmonth; Three integer arrays hold relative month date of blog
protected System.Web.UI.WebControls.Calendar Calendar1; And this is our Calendar control.

2. Below I will give the source of these two events, and explain its implementation below, if you do not understand, you can first look at the following instructions

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", "one", "two", "three", "four", "five", "six"};
CiNew.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Sunday;
Threadcurrent.currentculture = cinew;
}

The above code changes the display of the week name. You can change the name display simply by changing the value of the character array.

DayRender
private void Calendar1_dayrender (object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
{
The control occurs each day when it is created.

CalendarDay d = ((Dayrendereventargs) e). Day;
TableCell c = ((Dayrendereventargs) e). Cell;

Initializes an array of dates with blogs for the current month
if (intpremonth = 0)
{
Intpremonth = D.date.month; Note: The first month we get when the Calendar control is initialized is not the current month, but the month of the first one months
Intcurrentmonth = intpremonth+1;
if (intcurrentmonth>12)
intcurrentmonth=1;
Intnextmonth = intcurrentmonth+1;
if (Intnextmonth >12)
Intnextmonth = 1;
Arrpredays = Getarrayday (d.date.year,intpremonth); Get an array of dates with blogs for the first one months
Arrcurrentdays = Getarrayday (d.date.year,intcurrentmonth);//Get an array of dates with blogs for the month
Arrnextdays = Getarrayday (d.date.year,intnextmonth);//Get an array of dates for next month's 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=" +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=" +d.date.year+ "&month=" +
d.date.month+ "&day=" +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=" +d.date.year+ "&month=" +
d.date.month+ "&day=" +d.date.day+ ">" +d.date.day+ "</a>"));
}
j + +;
}
}
}

Date control a page can display a date of three months, the current month is complete, the previous January and the next month have a partial date. The DayRender event initializes the display of a specific date, where we add hyperlinks to the dates that have the content of the blog. So we need to initialize the three array, stored in the array for three consecutive months, the date of the blog. Then compare the current date in turn, and then add the link.

When using the DayRender event, you must not forget that it is executed once per date initialization, which means that the event to initialize a calendar control is executed 42 times, so simplify the operation as much as possible, not to do the repetitive database operations without judgment, I didn't notice at first, Two read-Library statements were written in the event, resulting in significant performance impact.

The following method is the one I used to get an array of dates.
Get an array of dates with blogs for the month
Private int[] Getarrayday (int intyear,int intmonth)
{
int[] Intarray = new INT[31];
Select the required records from the database and store the dates in an 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 if (Dr. GetDateTime (0). Day!= Intarray[i-1])
{
Intarray[i] = Dr. GetDateTime (0). Day;
i++;
}
}
Dr. Close ();
return intarray;
}

The concrete use effect may go to my blog to look: www.dever.cn


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.