Second Life of a Hungarian SharePoint Geek

December 3, 2009

Disabling the text box associated with the DateTimeControl from code

Filed under: Calendar, SharePoint — Tags: , — Peter Holpar @ 02:23
It may be sometimes useful to have a DateTimeControl where users can only pick values from the calendar but are not able to enter dates in the text box. Instead of disabling the textbox, it is a bit more elegant to hide it at all.

You can implement this as illustrated in the following code. It is important to note that using inline code in this example serves only simplicity and is not a recommended approach. I suggest you to put your code in code behind files, or even better in separate assemblies.

<%@ Inherits="System.Web.UI.Page" %>
<%@ Register TagPrefix="spuc" Namespace="Microsoft.SharePoint.WebControls"
Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c" %>

<form runat="server">
    <spuc:DateTimeControl runat="server" ID="DTC" />
</form>

<script runat="server" language="C#">
    void Page_Load(Object sender, EventArgs e)
    {
        ((TextBox)DTC.Controls[0]).Style.Add("display", "none");
    }
</script>

Originally I planned to have DTC.Controls[0].Visible = false; in Page_Load, but it had no effect as required, so we had to play with styles.

You can use this technique to create a custom picker-only DateTime field, or one, where you can decide which component (text box, picker or both) is visible.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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

Create a free website or blog at WordPress.com.

%d bloggers like this: