Posted by: R Manimaran | December 1, 2010

SharePoint WebPart: Make DateTime control TextBox ReadOnly

SharePoint WebPart: Make DateTime control TextBox ReadOnly

or

Disable Keypress on DateTimeControl

Last week I check a question in MSDN forum stating that

http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/474e4f9f-846c-45ae-b40b-07fa1ada7eec

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“);

this.Controls.Add(dtDob);

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

Advertisements

Responses

  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 asp.net 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.
    Khushi

  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:

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

Categories

%d bloggers like this: