The following describes the common attributes of the Calarder control:
SelectionMode: Specifies whether to select the day, week, and whole month. It is a CalendarSelectionMode enumeration;
SelectedDate: gets or sets the currently selected time, which is a DateTime structure;
VisibleDate: the date used to obtain or set the date displayed in the calendar. It is a DateTime structure;
PrevMonthText: indicates the symbol used to switch the previous month. It can be expressed as 'previous month' and is of the string type;
NextMonthText: indicates the symbol used to switch to the next month. It can be indicated by 'next month'. It is of the string type;
SelectMonthText: displays the symbols of the entire month. You can use 'select the entire month' to indicate the string type;
SelectWeekText: indicates the symbol used to display the entire week. It can be 'select', which is of the string type;
ShowGridLines: whether to display gridlines. It is of the Boolean type;
Common events:
SelectionChanged: triggered when a date is selected;
DayRender: triggered when the calendar control is loaded, which is equivalent to an OnRender event;
VisibleMonthChanged: all operations on the month are triggered;
Next, let's go to the question: how to implement the custom calendar control:
1. In the Page_Init method, instantiate a String-type two-dimensional array with the length of months and days, and then write the holiday of the corresponding date for later display, such:
The code is as follows: |
Copy code |
Private string [] [] arr; Protected void Page_Init (object sender, EventArgs e) { Arr = new string [13] []; // monthly, respectively For (int I = 0; I <13; I ++) { Arr [I] = new string [32]; // assign values to year and month } Arr [1] [1] = "New Year's Day "; Arr [2] [14] = "Valentine's Day "; Arr [3] [8] = "Women's Day "; Arr [3] [20] = "sister's birthday "; Arr [3] [12] = "Arbor Day "; Arr [4] [16] = "mom's birthday "; Arr [4] [29] = "My Birthday "; Arr [5] [1] = "Labor Day "; Arr [6] [1] = "Children's Day "; Arr [7] [1] = "party building Festival "; Arr [8] [1] = "Jianjun festival "; Arr [8] [10] = "dad's birthday "; Arr [9] [10] = "Teacher's Day "; Arr [10] [1] = "national day "; Arr [12] [24] = "Christmas Eve "; Arr [12] [25] = "Christmas "; } |
2. Perform operations on DayRender events;
The code is as follows: |
Copy code |
Protected void Calendar3_DayRender (object sender, DayRenderEventArgs e) { CalendarDay day = e. Day; // you can specify the current date. TableCell cells = e. Cell; // sets the table If (day. IsOtherMonth) // if the content is in another section, it is a date limit to clear the content Calendar in this section; { Cells. Controls. Clear (); } Else { Try { String txt = arr [day. Date. Month] [day. Date. Day]; // obtain the complete Month and day matching the array. If (! String. IsNullOrEmpty (txt) // You can check whether a holiday exists. { Cells. controls. add (new LiteralControl (string. format ("<br> <font color = red> {0} </font>", txt); // add the red string after the displayed date } } Catch (Exception) { Response. Write ("<font color = 'red'> } } } |
The display can be completed here, but we want to display the calendar of the link as the blog Garden does. For example, if we send a blog today, we need to highlight the date and have a hyperlink, how can this problem be achieved? I will go back to the second blog.