Sunday, May 9, 2010

Set ValidationGroup for DateTimeControl of Sharepoint

Sharepoint has enough built-in controls to develop custom application for it. It is wise to use sharepoint’s controls for sharepoint application. It gives you the flexibility to get same look and feel of sharepoint.

Once I needed a date time picker for one my sharepoint application. I looked for it into sharepoint control library and immediately got it. The date time picker of sharepoint is intuitive to use.

However, it has limitation. It has a built-in required field validation control. Unfortunately, you cannot set the ValidationGroup for the built-in validation control what is really annoying. Most of our real life scenarios, we had to set the validationGroup. The following code snippet shows a way to set validationGroup:

public class CusDateTimeControl : DateTimeControl
{
public CusDateTimeControl (string validationGroup)
{
ValidationGroup = validationGroup;
}
private string ValidationGroup { get; set; }

protected override void CreateChildControls()
{
base.CreateChildControls();
//Set validationgroup name.
m_validatorRequired.ValidationGroup = ValidationGroup;
}

}
The above code snippet is very plain and simple. It just inherits from DateTimeControl . The DateTimeControl class has a protected member (m_validatorRequired) for required field validation. Set the validationGroup of "m_validatorRequired" inside "CreateChildControl" method.

Hope this help!