Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calendar Control: Have todays date selected on page load

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

this.CalendarReportDay.SelectedDate = 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?

like image 233
Pieces Avatar asked Aug 03 '10 20:08

Pieces


2 Answers

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

like image 196
del.ave Avatar answered Nov 15 '22 10:11

del.ave


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>
like image 44
Tim Schmelter Avatar answered Nov 15 '22 12:11

Tim Schmelter