默认情况下,在ASP.Net日历控件中选择当前日期

Joe*_*orn 19 asp.net calendar

假设我有一个带有此日历控件的aspx页面:

<asp:Calendar ID="Calendar1" runat="server"  SelectedDate="" ></asp:Calendar>
Run Code Online (Sandbox Code Playgroud)

有没有什么我可以让SelectedDate默认使用当前日期,而不必使用代码隐藏?

Phi*_*eck 19

如果您已经在进行数据绑定:

<asp:Calendar ID="Calendar1" runat="server"  SelectedDate="<%# DateTime.Today %>" />
Run Code Online (Sandbox Code Playgroud)

会做的.这确实需要您正在进行Page.DataBind()调用(或父控件上的数据绑定调用).如果您没有这样做并且您绝对不希望页面上有任何代码隐藏,那么您将必须创建一个包含日历控件的usercontrol并设置其选定日期.


Kev*_*vin 16

DateTime.Now将无效,请改用DateTime.Today.


Sam*_*ary 7

我试图让日历默认选择日期并为用户突出显示.但是,我尝试使用上面的所有选项,但我只设法设置日历的选定日期.

protected void Page_Load(object sender, EventArgs e)
    Calendar1.SelectedDate = DateTime.Today;
}
Run Code Online (Sandbox Code Playgroud)

虽然它将SelectedDate设置为今天,但前面的代码没有突出显示选择.

但是,要选择并突出显示以下代码将正常工作.

protected void Page_Load(object sender, EventArgs e)
{
    DateTime today = DateTime.Today;
    Calendar1.TodaysDate = today;
    Calendar1.SelectedDate = Calendar1.TodaysDate;
}
Run Code Online (Sandbox Code Playgroud)

检查此链接:http://msdn.microsoft.com/en-us/library/8k0f6h1h(v = VS.85).aspx


Pas*_*dis 6

两种方式.

后期绑定

<asp:Calendar ID="planning" runat="server" SelectedDate="<%# DateTime.Now %>"></asp:Calendar>
Run Code Online (Sandbox Code Playgroud)

代码背后的方式(Page_Load解决方案)

protected void Page_Load(object sender, EventArgs e)
{
    BindCalendar();
}

private void BindCalendar()
{
    planning.SelectedDate = DateTime.Today;
}
Run Code Online (Sandbox Code Playgroud)

尽管如此,我强烈建议您使用BindMyStuff方式.单入口点更容易调试.但既然你似乎知道你的游戏,那么你就完全了.