Chris’s SharePoint Reflections

Just another WordPress.com weblog




  • Chris Zhong

    IT consultant Australia








SharePoint DateTime Control

Posted by chrissyz on July 9, 2009

SharePoint brings its own controls and DateTime control is one of them. You can definitely leverage it in your web parts and custom aspx pages. The control is located in the Microsoft.SharePoint.WebControls namespaces.

How to define it? First you need to add the following directive at the top of your application page:

<%@ Register TagPrefix=”SharePoint” Namespace=”Microsoft.SharePoint.WebControls” Assembly=”Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>

And then place the control in your page

<SharePoint:DateTimeControl runat=”server ID=”dtcDateTime”/>

Please notes that above is just a basic definition, for detailed properties, please check out this link:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.datetimecontrol_members.aspx

How to validate it?

<SharePoint:DateTimeControl runat=”server ID=”dtcDateTime”/>

<asp:CompareValidator ID=”valDateTime” runat=”server”

ForeColor=”Red”
ControlToValidate=”dtcDateTime$dtcDateTimeDate”
Type=”Date”
Operator=”DataTypeCheck”
ErrorMessage=”please enter a valid date” />

How to set a default value to it?
dtcDateTime.SelectedDate = new DateTime();
Check this link for the overloads:
http://msdn.microsoft.com/en-us/library/system.datetime.datetime.aspx

Important notice:

There is a Microsoft bug in SharePoint DateTime Control.
This DateTimeControl loses state during postbacks, specifically of the hour.
I struggled a bit to get this work around because it doesn’t work even if I put enableviewstate=”true” in the control.
However I happened to get this work by adding this line of code in a event that caused the postback. It could be any method/Event, in my case, it is SelectedIndexChanged event:
if (dtcDateTime< DateTime.Today)
{
//Don’t do anything
}
Just be doing this, it maintains the value of the control during postback.

Advertisements

9 Responses to “SharePoint DateTime Control”

  1. Djheath said

    You’re a star! I’ve been trying to solve this problem for ages! Thanks.

  2. MarkV said

    Chris…

    I think the syntax needs a touch up – What worked for me was
    if( dtcDateTime.SelectedDate < DateTime.Today) {}.
    Also, I needed to add this construct to all of my event handlers, referencing all of my DateTimeControls.

    I would have never thought of this fix, thanks for the tip!

  3. Paul said

    Thankyou Chris

    You saved my bacon with that little bug fix tip

  4. Bee said

    Chris,

    I spent hours figured this out before I saw your posting! Should have searched more thoroughly ;p.

    In my case, I have many controls which cause the post back. So wiring a method to the events weren’t enough for me. I had to set the following:

    EnableViewState=”true” AutoPostBack=”true” OnDateChanged=”Name_Of_Method_To_Call”

    and then in the “Name_Of_Method_To_Call”, I actually store the selectedDate in Session, and then set the value from the Session whenever there is a postback.

    Cheers 🙂

  5. Awdhesh said

    Chris,

    Actually i want to assign value to datetime Conrol.

    Value is coming from SQL 2005 database.

    DateTime DT = new DateTime();

    DT =

    Convert.ToDateTime(dr[“ActualStartDate”].ToString());//dr[“ActualStartDate”] is value which is comeing from SQL2005 database

    dateactualstat.ClearSelection();

    1) dateactualstat.SelectedDate = (DateTime)dt1.ToLongDateString();
    // Erroe : Error 2 Cannot convert type ‘string’ to ‘System.DateTime’
    2) dateactualstat.SelectedDate = DT // Its is also not working

    Please Suggest Me Answer

    Best Regards

    Awdhesh

    • Bee said

      What I did was to declare a structure for the record I want to retrieve from database, the structure consists of the date of type DateTime, I then retrieve the record from database and assign to this structure before assigning it to the DateTimeControl, the following is the codes:

      public MyStructure {
      private _EndDateTime;
      public DateTime EndDateTime
      {
      get { return _EndDateTime; }
      set { _EndDateTime = value; }
      }
      }

      MyStructure tEvent = new MyStructure();

      in the Data layer:

      tEvent.EndDateTime = (DateTime)reader[“StartDateTime”];

      in the Presentation Layer:

      DateTimeControl myControl; (this is a control arlready declared in the aspx page.

      myDtControl.SelectedDate = tEvent.EndDateTime;

      Cheers,

      Bee

  6. kimtuan8286 said

    How can I take datetimecontrol display in in firerox? Please help me

  7. Raja said

    Hello. Can you tell me how to position the sharepoint datetime control? I have been search everywhere but no luck. I know that the ASP date time control has a height, top, left properties. Please help.

  8. Free Styler said

    good

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: