Calendar控件用来显示日历,可以把日历当作日期选择器来使用或用来显示预定事件列表。 1. 选择多个日期 Calendar控件SelectionMode属性可以改变Calendar的行为,使你不仅可以选择日,也可以选择周或月。下面代码演示如何联合SelectionMode和SelectedDates属性来选择多个日期。<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void btn_Click(object sender, EventArgs e) { BulletedList1.DataSource = Calendar1.SelectedDates; BulletedList1.DataBind(); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Calendar ID="Calendar1" runat="server" SelectionMode="DayWeekMonth"></asp:Calendar><br /> <asp:Button ID="btn" runat="server" Text="Submit" OnClick="btn_Click" /><hr /> <asp:BulletedList ID="BulletedList1" runat="server" DataTextFormatString="{0:d}"> </asp:BulletedList> <!-- 使用 DataTextFormatString 属性可以为列表控件中的项提供自定义显示格式。 冒号前的值指定在从零开始的参数列表中的参数索引,此值只能设置为 0,因为每个单元格中只有一个值。 冒号后的字符指定值的显示格式。--> </div> </form> </body> </html> 效果: 2. 创建弹出式日期选择器 很简单,看代码和效果:<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void calEventDate_SelectionChanged(object sender, EventArgs e) { txtEventDate.Text = calEventDate.SelectedDate.ToString("d"); } protected void btnSubmit_Click(object sender, EventArgs e) { lblResult.Text = "You picked: " + txtEventDate.Text; } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <script type="text/javascript"> function displayCalendar() { var datePicker=document.getElementById('datePicker'); datePicker.style.display='block'; } </script> <style type="text/css"> #datePicker { display:none; position:absolute; border:solid 2px black; background:white; } </style> <title>无标题页</title> </head> <body> <form id="form1" runat=&#