1. Introduction
(1) In Calender, all selectable symbols are underlined because they are displayed as links in the browser.
If you can select a Day, month, or week, you must set the SelectionMode attribute (Day, DayWeek, DayWeekMonth)
(2) When you select a day or month, you can use OnSelectionChanged to trigger the event.
Use Calendar1.SelectedDate. tow.datestring (); to obtain the selected time point.
Use Calendar1.SelectedDate. Count. ToString (); to obtain the selected number of days.
2. Instance
Now we can use an instance to better understand the calendar control:
When you click TGIF, all Friday of the selected month is displayed on the calendar.
When you click Apply, the start and end dates are displayed on the calendar.
Calender. aspx. cs
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Public partial class myTest_Calender: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack ){
My_Calendar.VisibleDate = my_Calendar.TodaysDate;
// Map the month in the selected date to the drop-down box
Month_List.SelectedIndex = my_Calendar.VisibleDate.Month-1;
}
// Display to label
LblTodaysDate. Text = "Today is:" + my_calendar.todaysdate.to1_datestring ();
}
// Trigger the function when a date is selected
Protected void Calendar_Select (object sender, EventArgs e)
{
LblCountUpdate ();
LblSelectedUpdate ();
TxtClear ();
}
// Trigger the function in the drop-down box
Protected void Month_SelectedChange (object sender, EventArgs e)
{
My_Calendar.SelectedDates.Clear ();
LblSelectedUpdate ();
LblCountUpdate ();
// Reset the time
My_Calendar.VisibleDate = new DateTime (my_Calendar.VisibleDate.Year,
Int32.Parse (Month_List.SelectedItem.Value), 1 );
TxtClear ();
}
// Refactor function 01-Number of modification dates
Private void lblCountUpdate (){
LblCount. Text = "the Count of Selected:" + my_Calendar.SelectedDates.Count.ToString ();
}
// Refactor function 02-Clear Data
Private void txtClear (){
TxtEnd. Text = "";
TxtStart. Text = "";
}
// Refactor function 03-Modify date
Private void lblSelectedUpdate (){
If (my_Calendar.SelectedDate! = DateTime. MinValue)
LblSelctedDate. Text = "the selected day is:" +
My_calendar.selecteddate.tow.datestring ();
}
// Button 1: Display All Friday of the selected month
Protected void btnTgif_Click (object sender, EventArgs e)
{
Int currnetMonth = my_Calendar.VisibleDate.Month;
Int curretnYear = my_Calendar.VisibleDate.Year;
// Clear the original calendar position first
My_Calendar.SelectedDates.Clear ();
// Add the date to the calendar if it is Friday
For (int I = 1; I <= System. DateTime. DaysInMonth (
CurretnYear, currnetMonth); I ++ ){
DateTime datetime = new DateTime (curretnYear, currnetMonth, I );
If (datetime. DayOfWeek = DayOfWeek. Friday)
My_Calendar.SelectedDates.Add (datetime );
}
LblSelectedUpdate ();
LblCountUpdate ();
}
// Button 2:
Protected void btnRange_Click (object sender, EventArgs e)
{
Int currnetMonth = my_Calendar.VisibleDate.Month;
Int curretnYear = my_Calendar.VisibleDate.Year;
DateTime StartDate = new DateTime (curretnYear, currnetMonth, Int32.Parse (txtStart. Text ));
DateTime EndtartDate = new DateTime (curretnYear, currnetMonth, Int32.Parse (txtEnd. Text ));
My_Calendar.SelectedDates.Clear ();
My_Calendar.SelectedDates.SelectRange (StartDate, EndtartDate );
LblCountUpdate ();
LblSelectedUpdate ();
}
}
Calender. aspx
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Calender. aspx. cs" Inherits = "myTest_Calender" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Calender Control </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<H1> Calender Control <H2> <Asp: Calendar ID = "my_Calendar" runat = "server"
OnSelectionChanged = "Calendar_Select">
</Asp: Calendar>
<Br/>
<Asp: Label ID = "lblCount" runat = "server" Text = "Label"> </asp: Label>
<Br/>
<Asp: Label ID = "lblTodaysDate" runat = "server" Text = "Label"> </asp: Label>
<Br/>
<Asp: Label ID = "lblSelctedDate" runat = "server" Text = "Label"> </asp: Label>
<Table>
<Tr>
<Td> Select a month </td>
<Td>
<Asp: DropDownList ID = "Month_List" runat = "server"
AutoPostBack = "true"
OnSelectedIndexChanged = "Month_SelectedChange">
<Asp: ListItem text = "January" Value = "1"/>
<Asp: ListItem text = "February" Value = "2"/>
<Asp: ListItem text = "March" Value = "3"/>
<Asp: ListItem text = "April" Value = "4"/>
<Asp: ListItem text = "May" Value = "5"/>
<Asp: ListItem text = "June" Value = "6"/>
<Asp: ListItem text = "July" Value = "7"/>
<Asp: ListItem text = "Augut" Value = "8"/>
<Asp: ListItem text = "September" Value = "9"/>
<Asp: ListItem text = "October" Value = "10"/>
<Asp: ListItem text = "November" Value = "11"/>
<Asp: ListItem text = "December" Value = "12"/>
</Asp: DropDownList>
</Td>
<Td>
<Asp: Button ID = "btnTgif" runat = "server"
Text = "TGIF"
OnClick = "btnTgif_Click"/>
</Td>
</Tr>
<Tr>
<Td colspan = "2"> </td>
</Tr>
<Tr>
<Td colspan = "2"> <B> Day Range </B> </td>
</Tr>
<Tr>
<Td> Starting Day </td>
<Td> Ending Day </td>
</Tr>
<Tr>
<Td>
<Asp: TextBox ID = "txtStart" runat = "server"
Width = "25" MaxLength = "2"> </asp: TextBox>
</Td>
<Td>
<Asp: TextBox ID = "txtEnd" runat = "server"
Width = "25" MaxLength = "2"> </asp: TextBox>
</Td>
<Td>
<Asp: Button ID = "btnRange" runat = "server" Text = "Apply"
OnClick = "btnRange_Click" style = "height: 21px"/>
</Td>
</Tr>
</Table>
</Div>
</Form>
</Body>
</Html>
Summary:
(1. Use some refactoring to separate some function methods. Some of them are not completely separated yet.
(2 The date control also has the VisiblMonthChanged event to handle the comparison of calendar whether the month has been changed by the user, such as e. NewDate. Year and e. previusdate. Year.
(3 DayRender events, cell and Day can be used to present the special characteristics of dates, such as the colors of weekends and holidays,
E. Day. IsOtherMOnth e. Day. IsWeekend, etc.