解决方法
不,您无法轻松修改现有日历.但也许只需在日历上方放置一个表格行即可选择年份.
<table> <tr> <td> <asp:DropDownList id="drpCalMonth" Runat="Server" OnSelectedindexChanged="Set_Calendar" AutopostBack="true"></asp:DropDownList> <asp:DropDownList id="drpCalYear" Runat="Server" OnSelectedindexChanged="Set_Calendar" AutopostBack="true"></asp:DropDownList> </td> </tr> <tr> <td> <asp:Calendar id="cntCalendar" Runat="Server" Width="100%" /> </td> </tr> </table>
以下是填充年度和月份下拉列表的两种方法:
protected void Populate_MonthList() { //Add each month to the list var dtf = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat; for(int i=1; i<=12; i++) drpCalMonth.Items.Add(new ListItem(dtf.GetMonthName(i),i.ToString())); //Make the current month selected item in the list drpCalMonth.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true; } protected void Populate_YearList() { //Year list can be changed by changing the lower and upper //limits of the For statement for (int intYear = DateTime.Now.Year - 20; intYear <= DateTime.Now.Year + 20; intYear++) { drpCalYear.Items.Add(intYear.ToString()); } //Make the current year selected item in the list drpCalYear.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true; }
您可以从Page_Load初始化列表:
protected void Page_Load(object sender,EventArgs e) { if (!IsPostBack) { Populate_MonthList(); Populate_YearList(); } }
最后,这里是DropDownLists的SelectedindexChanged事件的事件处理程序,它设置了新的Date:
protected void Set_Calendar(object Sender,EventArgs e) { int year = int.Parse(drpCalYear.SelectedValue); int month = int.Parse(drpCalMonth.SelectedValue); cntCalendar.TodaysDate = new DateTime(year,month,1); }
[测试]
灵感来自:http://www.4guysfromrolla.com/articles/090104-1.aspx(VB)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。