开发者

Calendar Control: Have todays date selected on page load

开发者 https://www.devze.com 2023-01-10 06:23 出处:网络
I have a calendar control on a asp .net webform.In the Pag_Load event I have this.CalendarReportDay.SelectedDate =开发者_Go百科 DateTime.Now;

I have a calendar control on a asp .net webform. In the Pag_Load event I have

this.CalendarReportDay.SelectedDate =开发者_Go百科 DateTime.Now;

Which sets the Calendar's Selected Date but todays date is not highlighted on the calendar.

Does anyone know how to get todays date to be selected?


SelectedDate will set the calendar's date, but that does not mean it will highlight it.

One issue is that DateTime.Now includes the time whereas the calendar needs ONLY the date to work as expected, so you can use DateTime.Today instead, e.g.

myCalendar.SelectedDate = DateTime.Today

To show the date (i.e. to get the calendar to display the correct month and year needed to show the selected date) use VisibleDate, e.g.

myCalendar.VisibleDate = dateToUse;

For more details, have a look at:

http://www.devtoolshed.com/content/how-highlight-day-aspnet-calendar-control-selecteddate-property


You have to set

this.CalendarReportDay.SelectedDate = DateTime.Now.Date;

The Date property at the end is important, otherwise the time component of DateTime.Now will prevent the selection. Then it gets the applied SelectedDayStyle, f.e.

<asp:Calendar ID="CalendarReportDay" runat="server">
   <SelectedDayStyle Font-Size="X-Large" />
</asp:Calendar>
0

精彩评论

暂无评论...
验证码 换一张
取 消