Custom DateTimeControl Formatting

A problem that I recently encountered working on a MOSS project was the inability to customize date formating of the Microsoft.SharePoint.WebControls.DateTimeControl for a setting that is outside the realm of the default formatting for a culture. The default culture short date formatting was dd/mm/yyyy for en-AU but instead I needed it to display in dd MMM yyyy. This control has a LocaleId property but no way of simply customizing the formatting of the selected date value.
 
All I could find on the net that would work was to redefine the culture setting in the registry using the CultureAndRegionInfoBuilder (as mentioned here: http://sharepointex.blogspot.com/2007/09/how-to-modify-date-format-in-sharepoint.html). This was not ok for me because I was deploying into a shared environment and this would break someone else’s culture-sensitive formatting. So I created a derived class from the DateTimeControl and overrode the OnPreRender event so that I could handle the formatting myself and this did the trick. Here is my code for doing so:
 

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace TestDerivedControl
{
    [Guid("619b97bb-1c70-426e-b3ec-f76c5324be3a")]
    TestDerivedControl : System.Web.UI.WebControls.WebParts.WebPart
    {
        public TestDerivedControl()
        {
            this.ExportMode = WebPartExportMode.All;
        }
        protected override void Render(HtmlTextWriter writer)
        {
            this.RenderChildren(writer);
        }
       
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            // Add the controls here
            CustomAUDatePicker obj = new CustomAUDatePicker();
           
            obj.ID = "dtcDate";
            obj.AutoPostBack = true;
            obj.DateOnly = true;
            obj.Visible = true;
            obj.SelectedDate = DateTimeNow;
            this.Controls.Add(obj);
        }
    }
    public class CustomAUDatePicker : Microsoft.SharePoint.WebControls.DateTimeControl
    {
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            TextBox box = (TextBox)this.Controls[0];
            DateTime dt = DateTime.Parse(box.Text);
            box.Text = dt.ToString("dd MMM yyyy");
       
        }
          
    }
}
 
Advertisements

,

  1. #1 by Matt on February 8, 2008 - 4:08 am

    What language is that?
     
    -Matt

  2. #2 by Jecoso on October 17, 2008 - 8:37 am

    Thank you for you post, it is very helpful to me.But how to use these codes?I think I should build them to a dll file, but how to do as you said "overrode the OnPreRender event"….Sorry for my stupidity~~~Thanks in advance.

  3. #3 by Jecoso on October 17, 2008 - 8:43 am

    In additional, can this code effect the whole SharePoint application?Or only a SharePoint site collection?Thank you agian^^

  4. #4 by Nigel on August 4, 2011 - 8:04 am

    A little old maybe – but a useful post.
    Just tried to implement the custom time/date format and ran into validation problems. For some reason the validation code fails 😦

    • #5 by clineer on August 4, 2011 - 11:56 am

      I think the code worked back on .NET 2.0 – I am not sure if it still works on later versions of .NET. What version of .NET are you using?

  5. #6 by ganar dinero en casa yahoo on March 12, 2013 - 9:57 am

    When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each
    time a comment is added I get three emails with the same comment.
    Is there any way you can remove me from that service?
    Thanks!

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: