SharePoint:DateTimeControl – Check if control is empty

SharePoint offers a DateTime picker control that you can use in your web parts and application pages. This control has the name DateTimeControl and is located in the Microsoft.SharePoint.WebControls namespace of the Microsoft.SharePoint.dll.

You can place it on your application page as follows: <SharePoint:DateTimeControl
ID=”DateTimeControl1″ runat=”server”
/>

After having added the required directive at the top of the page:

<%@
Register
TagPrefix=”SharePoint” Namespace=”Microsoft.SharePoint.WebControls”


Assembly=”Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”
%>

 

 

If you want to add the date picker control from code.


DateTimeControl datetimepicker;


protected
override
void CreateChildControls()

{


base.CreateChildControls();

datetimepicker = new
DateTimeControl();

datetimepicker.ID = “datetimeControl1”;


this.Controls.Add(datetimepicker);

 


// initialize other controls

}

 

If in your code you want to test if the user selected a date in the DateTimeControl, you have to use the IsDateEmpty property instead of the SelectedDate.

If the user didn’t enter a date, the SelectedDate property will return today’s date.

protected
void Submit()

{


if (!datetimepicker.IsDateEmpty)

{


var selectedDate = datetimepicker.SelectedDate;

 


// continue your function

}

}

 

About the Author

Dany Hoyek
I am a Computer and Communication Engineer and Consultant. I have been leading, developing and managing millions of dollars worth of software development projects.