Writing New Controls

Writing new controls is often desired by advanced programmers, because they want to reuse the code that they write for dealing with complex presentation and user interactions.

In general, there are two ways of writing new controls: composition of existing controls and extending existing controls. They all require that the new control inherit from TControl or its child classes.

Composition of Existing Controls

Composition is the easiest way of creating new controls. It mainly involves instantiating existing controls, configuring them and making them the constituent components. The properties of the constituent components are exposed through subproperties.

One can compose a new control in two ways. One is to extend TCompositeControl and override the TControl::createChildControls() method. The other is to extend TTemplateControl (or its child classes) and write a control template. The latter is easier to use and can organize the layout constituent components more intuitively, while the former is more efficient because it does not require parsing of the template.

As an example, we show how to create a labeled textbox called LabeledTextBox using the above two approaches. A labeled textbox displays a label besides a textbox. We want reuse the PRADO provided TLabel and TTextBox to accomplish this task.

Composition by Writing Templates

We need two files: a control class file named LabeledTextBox.php and a control template file named LabeledTextBox.tpl. Both must reside under the same directory.

Like creating a PRADO page, we can easily write down the content in the control template file.

<com:TLabel ID="Label" ForControl="TextBox" />
<com:TTextBox ID="TextBox" />

The above template specifies a TLabel control named Label and a TTextBox control named TextBox. We would to expose these two controls. This can be done by defining a property for each control in the LabeledTextBox class file. For example, we can define a Label property as follows,

class LabeledTextBox extends TTemplateControl {
    public function getLabel() {
        $this->ensureChildControls();
        return $this->getRegisteredObject('Label');
    }
}

In the above, the method call to ensureChildControls() ensures that both the label and the textbox controls are created (from template) when the Label property is accessed. The TextBox property can be implemented similarly.

Composition by Overriding createChildControls()

For a composite control as simple as LabeledTextBox, it is better to create it by extending TCompositeControl and overriding the createChildControls() method, because it does not use templates and thus saves template parsing time.

Complete code for LabeledTextBox is shown as follows,

class LabeledTextBox extends TCompositeControl {
    private $_label;
    private $_textbox;
    public function createChildControls() {
        $this->_label=new TLabel;
        $this->_label->setID('Label');
        // add the label as a child of LabeledTextBox
        $this->getControls()->add($this->_label);
        $this->_textbox=new TTextBox;
        $this->_textbox->setID('TextBox');
        $this->_label->setForControl('TextBox');
        // add the textbox as a child of LabeledTextBox
        $this->getControls()->add($this->_textbox);
    }
    public function getLabel() {
        $this->ensureChildControls();
        return $this->_label;
    }
    public function getTextBox() {
        $this->ensureChildControls();
        return $this->_textbox;
    }
}

Using LabeledTextBox

To use LabeledTextBox control, first we need to include the corresponding class file. Then in a page template, we can write lines like the following,

<com:LabeledTextBox ID="Input" Label.Text="Username" />

In the above, Label.Text is a subproperty of LabeledTextBox, which refers to the Text property of the Label property. For other details of using LabeledTextBox, see the above online examples.

Extending Existing Controls

Extending existing controls is the same as conventional class inheritance. It allows developers to customize existing control classes by overriding their properties, methods, events, or creating new ones.

The difficulty of the task depends on how much an existing class needs to be customized. For example, a simple task could be to customize TLabel control, so that it displays a red label by default. This would merely involves setting the ForeColor property to "red" in the constructor. A difficult task would be to create controls that provide completely innovative functionalities. Usually, this requires the new controls extend from "low level" control classes, such as TControl or TWebControl.

In this section, we mainly introduce the base control classes TControl and TWebControl, showing how they can be customized. We also introduce how to write controls with specific functionalities, such as loading post data, raising post data and databinding with data source.

Extending TControl

TControl is the base class of all control classes. Two methods are of the most importance for derived control classes:

  • addParsedObject() - this method is invoked for each component or text string enclosed within the component tag specifying the control in a template. By default, the enclosed components and text strings are added into the Controls collection of the control. Derived controls may override this method to do special processing about the enclosed content. For example, TListControl only accepts TListItem components to be enclosed within its component tag, and these components are added into the Items collection of TListControl.
  • render() - this method renders the control. By default, it renders items in the Controls collection. Derived controls may override this method to give customized presentation.
Other important properties and methods include:
  • ID - a string uniquely identifying the control among all controls of the same naming container. An automatic ID will be generated if the ID property is not set explicitly.
  • UnqiueID - a fully qualified ID uniquely identifying the control among all controls on the current page hierarchy. It can be used to locate a control in the page hierarchy by calling TControl::findControl() method. User input controls often use it as the value of the name attribute of the HTML input element.
  • ClientID - similar to UniqueID, except that it is mainly used for presentation and is commonly used as HTML element id attribute value. Do not rely on the explicit format of ClientID.
  • Enabled - whether this control is enabled. Note, in some cases, if one of the control's ancestor controls is disabled, the control should also be treated as disabled, even if its Enabled property is true.
  • Parent - parent control of this control. The parent control is in charge of whether to render this control and where to place the rendered result.
  • Page - the page containing this control.
  • Controls - collection of all child controls, including static texts between them. It can be used like an array, as it implements Traversable interface. To add a child to the control, simply insert it into the Controls collection at appropriate position.
  • Attributes - collection of custom attributes. This is useful for allowing users to specify attributes of the output HTML elements that are not covered by control properties.
  • getViewState() and setViewState() - these methods are commonly used for defining properties that are stored in viewstate.
  • saveState() and loadState() - these two methods can be overriden to provide last step state saving and loading.
  • Control lifecycles - Like pages, controls also have lifecycles. Each control undergoes the following lifecycles in order: constructor, onInit(), onLoad(), onPreRender(), render(), and onUnload. More details can be found in the page section.

Extending TWebControl

TWebControl is mainly used as a base class for controls representing HTML elements. It provides a set of properties that are common among HTML elements. It breaks the TControl::render() into the following methods that are more suitable for rendering an HTML element:

  • addAttributesToRender() - adds attributes for the HTML element to be rendered. This method is often overridden by derived classes as they usually have different attributes to be rendered.
  • renderBeginTag() - renders the opening HTML tag.
  • renderContents() - renders the content enclosed within the HTML element. By default, it displays the items in the Controls collection of the control. Derived classes may override this method to render customized contents.
  • renderEndTag() - renders the closing HTML tag.

When rendering the openning HTML tag, TWebControl calls getTagName() to obtain the tag name. Derived classes may override this method to render different tag names.

Creating Controls with Special Functionalities

If a control wants to respond to client-side events and translate them into server side events (called postback events), such as TButton, it has to implement the IPostBackEventHandler interface.

If a control wants to be able to load post data, such as TTextBox, it has to implement the IPostBackDataHandler interface.

If a control wants to get data from some external data source, it can extend TDataBoundControl. TDataBoundControl implements the basic properties and methods that are needed for populating data via databinding. In fact, controls like TListControl, TRepeater are TDataGrid are all derived from it.