Tuesday, September 16, 2014

configure SharePoint:DateTimecontrol to run server side for dynamically generated controls

I'm sure you may wonder, why do we care about Server side firing of default validation comes with SharePoint:DateTime Control. We just can enable IsRequired = "True" and configure <control>.ErrorMessage with appropriate styles.

Yes you are correct. However think about a scenario that client want to have server side validators plus client side validators like SharePoint:Datetime control. If this is the case, can you imagine the main issue. When end user clicks on "Submit" button first application fires client side validation. Then after fires the server side validators within separate post back. This issue would be a great headache for sure.

So let's dig into the issue using my scenario. All fields were generating dynamically using the SharePoint list fields and controls were in <tr><td></td></tr> sets. Ultimately no server control markup to configure attributes. then I have created the DateTime control object from the BaseFieldControl(Microsoft.SharePoint.WebControls) object get passed.

DateTimeField dateTimeField = (DateTimeField)control;

Then I have disabled IsRequired from there.

dateTimeControl.IsRequiredField = false;


After that I have added a label control dynamically (initially setting styles to hide on load), while adding the control id to the Session. So ultimately I can access the control through button code behind.

Finally, get the control id from session variables and check whether datetimecontrol is empty or not. If empty we will reset styles to show the error message label and hide if not. This was my simple solution within my context. Hope will help someone and thought to share.

No comments:

Post a Comment