Dayrender event of Calendar Control
DayRender is a relatively important event for the Calendar control. It can be used to "length" in all the date in the Calendar control. This event uses a DayRendReventArgs object as its parameters. The latter includes two properties: Cell and Day. In MSDN, there is a description of these two attributes:
Attributes
Description
Cell
Get TableCell objects indicating the cells present in the Calendar control
DAY
Gets Calendarday that represents the date rendered in the Calendar control
Use them to complete some operations:
1). Date of non-designated month in the Calendar control
Take the current time as an example, the month is designated as DateTime.now.month. First set the Calendar control's VisibleDate property to datetime.now, then write the DayRender event of the Calendar control, a possible writing method is as follows:
/ * Step1: Set the VisibleDate property of the Calendar control in Page_Load, or set it through the IDE, or manually write .aspx to complete * /
/ * Step2: Write the processing of DayRender events * /
// Declare a variable to save the current displayed month
Private Int IntCurrentMonth = datetime.now.month;
Private void Calendar1_dayrender (Object Sender, System.Web.ui.WebControls.dayrenderEventArgs E) {
IF (e.day.date.mont! = INTMONTH) // If not the specified month
E.Cell.Text = ""; // set the TEXT attribute of the corresponding TableCell object to ""
}
This code can work well. After running, we can see the results such as the following figure:
However, if the prepletar control is not "", when you click on them, when you click on them, due to the previous control logic, you may not get the result of the expected results. For example:
2004-12
2005-1
The desired result is:
2004-12
2005-1
In order to get this effect, we can use another event prerender of the Calendar control. In this event, set the variable INTMONTH to Calendar.VisibleDate.Month:
Private void Calendar1_prender (Object Sender, System.Eventargs E) {
THIS.INTMONTH = this.calendar1.visibledate.month;
}
2) Adding a note for a specific date
In order to complete the additional note to the specified date, you can first abstract a Class, such as DateMemo, used to record a note corresponding to a date. Specifically, as follows:
INTERNAL CLASS DATEMEMO {
Private datetime dt; //
Private string memo; // Corresponding note
//Constructor
Public DateMemo (DateTime DT, String Memo) {
THIS.DT = DT;
THIS.MEMO = MEMO;
}
/ / Provide attribute
Public String Memo {
Get {
Return this.Memo;
}
SET {
THIS.MEMO = VALUE;
}
}
Public DateTime Date {
Get {
Return this.dt;
} // ONLY GETTER, NO SETTER
}
}
// This class is not perfect, and it can also overwrite the basic class method such as Equals, GetHashCode.
The following is a need for a container to store those dates that need to add a note. Of course, because there is a DateMemo class, this container is used to store the object is the DateMemo object. Here, the simplest container - array, is DATEMEMO []. One problem is: Where can I get these DATEMEMO objects? In fact, the data source is unfounded, from the file, the data is extracted, in order to highlight the theme, here "Write": Private DateMEMO [] data; / / Store DateMemo array
Private string [] memo; // date remarks
Private void Page_load (Object Sender, System.Eventargs E) {
IF (! this.ispostback) {// First load
This.calendar1.visibleDate = datetime.now;
Memo = new string [] {
"Database EXAM",
"AccompLish the BOOK",
"Body building",
"Do Research Work on Neural Network",
"Having a face-to-face with eNGLish Teacher",
}
// Build a DateMEMO array, you can load from other data sources, if you may not use the MEMO array, follow the actual situation to complete the load
Data = new datememo [memo.length];
For (int i = 0; i Data [i] = new datememo (datetime.now.addday, MEMO [i]); // Set the date of the continuous data.length starting with DateTime.now to 2 days to add the date you need to add a note } } // dayrender event Private void Calendar1_dayrender (Object Sender, System.Web.ui.WebControls.dayrenderEventArgs E) { / * If you expect to display only the date of the current month, use 1) IF (e.day.date.month! = Intmonth) E.Cell.Text = "" Else { * / // SpecialdayIndex is a auxiliary function that determines if the parameter is a date that needs to add a note. Int index = this.SpecialdayIndex (e.day.date); IF (INDEX> = 0) { // If you need to add a date of addition / / Complete the addition action of the note here E.cell.backcolor = color.fromname ("caver"); // Set the background color of the Cell E.cell.Tooltip = this.data [index] .MEMO; // Set Cell's Tooltip } / * } * / } Among them, the implementation of SpecialdayIndex is: // Decision parameter is a date that needs to add a note // Return value: -1 indicates that the parameter is not a date that needs to add a note. / / Non-negative integer indication parameters is a date that needs to add a note, the return value is the serial number in the DATA Private Int SpecialdayIndex (DateTime DT) { IF (this.data == null) return -1; INT INDEX = 0; Foreach (DateMemo DM in this.data) { IF (Dt.Year == DM.date.Year) && (DT.MONTH == DM.DATE.MONTH) && (dt.day == DM.DATE.DAY)) // The you need to judge yourself, you can't use (DT == DM.Date) or (Dt.equals (DM.DATE)) Return INDEX; INDEX ; } Return -1; } A result of running the program is as follows: Among them, DateTime.Now is 2004-11-4, when the cursor is placed on 2004-11-8, its corresponding tooltip is displayed. You can set other properties.