2

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
  • 119
  • 1
  • 3
  • 15

2 Answers2

6

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

if(!IsPostBack)
   calDate.SelectedDate = DateTime.Today;
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 1
    Can i have anything like the above in source page itself @Tim – Padmanaban Aug 21 '15 at 10:55
  • 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! – Padmanaban Aug 21 '15 at 11:21
  • 2
    @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](https://msdn.microsoft.com/en-us/library/ms178366.aspx): `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. – Tim Schmelter Aug 21 '15 at 11:34
  • Agree with you @Tim. Thanks for all you provided here! I would implement the same as you suggested. – Padmanaban Aug 21 '15 at 11:54
  • Will the `txtDate` control text change accordingly ? – Ishikawa Jan 08 '21 at 11:02
1

Another example using @Hutchonoid approach: example below illustrate how to correctly use ajaxcontrolTookKit CalendarExtender.

<ajaxControlToolKit:CalendarExtender runat="server"
           id="cal1"
           TargetControlID="txtDateFrom"
           CssClass="MyCalendar ajax__calendar ajax__calendar_hover"
           Format="dd/MM/yyyy"
           PopupButtonID="imgControl"
           PopupPosition="BottomRight"
           SelectedDate="<%# DateTime.Today %>"  >
           </ajaxControlToolKit:CalendarExtender>
           <asp:TextBox  Type="text" ID="txtDateFrom" runat="server"></asp:TextBox>
           <asp:ImageButton ID="imgControl" runat ="server"  ImageUrl 
           ="~/_icons/ajaxcalendar.png" />

Hope the above code snip-it helps or at least clarify the concept.

Waheed Rafiq
  • 460
  • 1
  • 6
  • 17