SP DateTimeControl common method

Here you can find common useful extension method to create Sharepoint DateTimeControl.

/// <summary>
/// Get common SP DateTimeControl with current DateOnly settings and MethodToProcessDateChanged
/// </summary>
public static DateTimeControl Cd(this bool DateOnly, bool AutoPostBack = false,
DateTime? SelectedDate = null, Func<bool> MethodToProcessDateChanged = null)
{
     var d = new DateTimeControl();
     d.DateOnly = DateOnly;
     d.AutoPostBack = AutoPostBack;
     if (SelectedDate != null)
     d.SelectedDate = SelectedDate.Value;
     if (MethodToProcessDateChanged != null)
          d.DateChanged += (o, e) => { MethodToProcessDateChanged();};
     return d;
}

Usage is very simple :

//  create simple DateTimeControl
var dt = true.Cd(false, DateTime.Now);
//  create DateTimeControl with MyCustomMethod to process Date Changed Event  
var dt2 = true.Cd(true, DateTime.Now, () => { MyCustomMethod(); });   

Leave a Reply