Sharepoint WebControl Part I: Reusing OWS Controls in Custom Webpart
(This articles has already been posted in www.msd2d.com. Now I post it again in my own blog.)
One of Microsoft.Sharepoint namespaces that rarely been talked about is Microsoft.Sharepoint.WebControl. Most of the controls in this namespace are derived from System.Web.UI.Control class - which is the base class for the controls in a web page. I will not discuss all controls but our interest is the OWS* control. There are 5 OWS* control in this namespace :
Look at the order! The OWSForm is container for OWSControl - a base class for the other OWS*Field and OWSSubmitButton.
Basically the OWSForm is responsible to create form and OWS*Fields are the input definition. OWSSubmitButton is then create relevant submit action for specific OWSForm.
Reusing OWS Control
Let's the creating webpart task leaves behind - because many examples around the web. We will focus our discussion on how to use the OWSControl.
First, remember the order !
If you need to use any of OWS*Field control then the container (OWSForm) must exist. You can create OWSForm object by calling
OWSForm owsForm = new OWSForm();
and the OWSDateField objectby calling
OWSDateField dateFld = new OWSDateField();
Second, always submit the OWSForm
When the values need to be submitted - we must make sure that we submit the correct form. The easiest way is using OWSSubmitButton, because it will create relevant submit script for that control.
OWSSubmitButton submitBtn = new OWSSubmitButton();
Third, add everything to the containers
The next step is to add all child components to the container,
Although you can directly add OWS*Field to the WebPart control - but it's not suggested as you disobey the hierarchy. You can also layout the components before adding them to the containers.
Last, OWS*Field.Value is not the value you expect
So, you can get the correct value using
Since you need to have control's UniqueID then you MUST PUT THE LAST STATEMENT after all controls has been added to it parent!
dateFld.Value = Page.Request.Params[dateFld.UniqueID]
will create same effect as EnableViewState = true.
To use OWS Control we need OWSForm as containers and follow the hierarchy to build it up in webpart.