views:

7299

answers:

4

Let's say I have an aspx page with this calendar control:

<asp:Calendar ID="Calendar1" runat="server"  SelectedDate="" ></asp:Calendar>

Is there anything I can put in for SelectedDate to make it use the current date by default, without having to use the code-behind?

+3  A: 

If you are already doing databinding:

<asp:Calendar ID="Calendar1" runat="server"  SelectedDate="<%# DateTime.Now %>" />

Will do it. This does require that somewhere you are doing a Page.DataBind() call (or a databind call on a parent control). If you are not doing that and you absolutely do not want any codebehind on the page, then you'll have to create a usercontrol that contains a calendar control and sets its selecteddate.

Philip Rieck
A: 

why not set it in Page_Load?

Slace
+1  A: 

Two ways of doing it.

Late binding

<asp:Calendar ID="planning" runat="server" SelectedDate="<%# DateTime.Now %>"></asp:Calendar>

Code behind way (Page_Load solution)

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

private void BindCalendar()
{
    planning.SelectedDate = DateTime.Now;
}

Altough, I strongly recommend to do it from a BindMyStuff way. Single entry point easier to debug. But since you seems to know your game, you're all set.

Pascal Paradis
Unfortunately, this a very simple page and there's no other databinding anywhere. In fact, if I can get this working the way I want there won't even a code-behind.
Joel Coehoorn
+2  A: 

DateTime.Now will not work, use DateTime.Today instead.

Kevin