Friday 27 February 2009

SharePoint DateTimeControl Validation

I have been using a DateTimeControl for a custom aspx Page in my Sharepoint site, this control is really nice and having so many features inbuilt.
However, I noticed one small drawback with it - Validation.Internally the DateTimeControl of SharePoint is just a calendar dropdown and a text box. Also, they already implemented a simple required field validation by just using the IsRequiredField property. However, if you try to put an asp.net validator (i.e.: CompareValidator, RegularExpressionValidtator, etc...) on it, it says it can't find the control. This is happening because the DateTimeControl is just a wrapper around this textbox and if you set the Validator ControlToValidate property to the DateTimeControl it isn't smart enough to know to really validate against the actual textbox.So, how do we accomplish this. Actually, it is pretty easy - SharePoint is giving an ID to that Textbox programatically and this ID is the same name as your control with the word "Date" added to the end.So, in your ControlToValidate just put "{nameofcontrol}${nameofcontrol}Date".

Here is an example:




No comments: