How to set current date in CalendarExtender

10,138

You just need to assign it in codebehind, for example in Page_Load:

if(!IsPostBack)
   calDate.SelectedDate = DateTime.Today;
Share:
10,138
Padmanaban
Author by

Padmanaban

.Net Developer, working as a project engineer in Wipro Technologies, Pune

Updated on June 26, 2022

Comments

  • Padmanaban
    Padmanaban almost 2 years

    Requirement is simple.

    How to set current date in CalendarExtender control.

    <cal:CalendarExtender ID="calDate" runat="server" SelectedDate="2008-01-01" TargetControlID="txtDate" CssClass="CalendarExtender" Format="yyyy/MM/dd">
    

    Here the selected date is 2008-01-01. I need to show current date instead of 2008-01-01

    Appreciate your assistance

  • Padmanaban
    Padmanaban over 8 years
    Can i have anything like the above in source page itself @Tim
  • Padmanaban
    Padmanaban over 8 years
    I can follow the same what you have suggested @Tim but i just wanna know do we have any other options to get the same result over source page so i no need to check other conditions at codebehind. Hope the above make sense!
  • Tim Schmelter
    Tim Schmelter over 8 years
    @Padmanaban: in general codebehind is for logic and aspx for layout. But apart from that it's not easy to get the same result with inline aspx. Hutchonoid has posted an (now deleted) answer which showed a way with a data binding expression: SelectedDate="<%# DateTime.Today %>". The problem is that you can use that only if the this control or one of it's parent controls is databound. Otherwise it doesn't work. Another reason to use the codebehind.
  • Padmanaban
    Padmanaban over 8 years
    Agree with you @Tim. Thanks for all you provided here! I would implement the same as you suggested.
  • Ishikawa
    Ishikawa over 3 years
    Will the txtDate control text change accordingly ?