Posted by: R Manimaran | December 1, 2010

SharePoint WebPart: Make DateTime control TextBox ReadOnly

SharePoint WebPart: Make DateTime control TextBox ReadOnly


Disable Keypress on DateTimeControl

Last week I check a question in MSDN forum stating that

A DateTime control is placed in a webpart. We need to allow the users to select the date only using the Calendar Icon and prevent typing the value in the TextBox.

Here is the Solution.

In the webpart, add the following lines while declaring your DateTimeControl in CreateChildControls

dtDob = new DateTimeControl();

dtDob.DateOnly = true;

((TextBox)(dtDob.Controls[0])).Attributes.Add(“readonly“, “readOnly“);


Now test the Webpart. The textbox is now changed into a readOnly field.



  1. Thanks Man ..this was helpful…

    I was doing ((TextBox)strDate.Controls[0]).ReadOnly = true;
    but this was resetting the date back to default date even if i selected another one..but ur solution worked.

  2. Thank you so much for your post. It did work. I did try couple of solution putting custom validator, javascripts, etc.. to put the validation but nothing worked. After making it readonly i no need to put various kinds of validation.

    Thanks again.

  3. Excellent fix. This worked great for me. Many Many thx.

Leave a Reply

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

You are commenting using your 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: