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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With