开发者

ASP.NET 4 Calendar control doesn't show SelectedDate

I have an ASP.NET 4 calendar display problem. I wa开发者_C百科nt to programmatically set the calendar. So, I put this code in the void Page_Load(object sender, EventArgs e) function:

Calendar1.SelectedDate = DateTime.Parse("2011-07-25");

it works fine. But, it displays the current month of the calendar (August) instead of the date I selected (July). I have to select back one month to see the date that I programmatically assigned.

ASP.NET 4 Calendar control doesn't show SelectedDate

ASP.NET 4 Calendar control doesn't show SelectedDate

What should i do?

Thanks!


You need to set the VisibleDate property to the month that you want it to show =)


There is a gotcha that you should be aware of.

If you set the selected date to DateTime.Now or something based on it such as DateTime.Now.AddYears(1) then the selected date will not be highlighted in the calendar even though it is selected.

You need to remove the time component using DateTime.Now.Date so that the calendar can do the comparison correctly when rendering. It took me a bit of debugging in the DayRenderEventHandler to figure that out.

I hope that somebody finds it useful.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜