简体   繁体   English

ASP.NET 4 日历控件不显示 SelectedDate

[英]ASP.NET 4 Calendar control doesn't show SelectedDate

I have an ASP.NET 4 calendar display problem.我有一个 ASP.NET 4 日历显示问题。 I want to programmatically set the calendar.我想以编程方式设置日历。 So, I put this code in the void Page_Load(object sender, EventArgs e) function:所以,我把这段代码放在void Page_Load(object sender, EventArgs e)函数中:

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.我必须选择返回一个月才能看到我以编程方式分配的日期。

在此处输入图片说明在此处输入图片说明

What should i do?我应该怎么办?

Thanks!谢谢!

您需要将VisibleDate属性设置为您希望它显示的月份 =)

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.如果您将所选日期设置为DateTime.Now或基于它的其他内容(例如DateTime.Now.AddYears(1)则即使选择了所选日期,它也不会在日历中突出显示。

You need to remove the time component using DateTime.Now.Date so that the calendar can do the comparison correctly when rendering.您需要使用DateTime.Now.Date删除时间组件,以便日历在渲染时可以正确进行比较。 It took me a bit of debugging in the DayRenderEventHandler to figure that out.我在DayRenderEventHandler进行了一些调试才弄明白。

I hope that somebody finds it useful.我希望有人觉得它有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM