• No results found

TRepeater

In document PRADO v3.1 Quickstart Tutorial 1 (Page 138-143)

TRepeater displays its content defined in templates repeatedly based on the given data specified by the DataSource or DataSourceID property. The repeated contents can be retrieved from the Items property. Each item is created by instantiating a template and each is a child control of the repeater.

Like normal control templates, the repeater templates can contain static text, controls and special tags, which after instantiation, become child contents of the corresponding item. TRepeater defines five templates for different purposes,

• HeaderTemplate - the template used for displaying content at the beginning of a repeater; • FooterTemplate - the template used for displaying content at the end of a repeater;

• ItemTemplate - the template used for displaying every repeater item. If AlternatingItemTemplate is also defined, ItemTemplate will be used for displaying item 1, 3, 5, etc.

• AlternatingItemTemplate - the template used for displaying every alternating repeater item (i.e., item 2, 4, 6, etc.)

• SeparatorTemplate - the template used for displaying content between items.

To populate data into the repeater items, set DataSource to a valid data object, such as array, TList, TMap, or a database table, and then call dataBind() for the repeater. That is,

class MyPage extends TPage {

public function onLoad($param) { parent::onLoad($param); if(!$this->IsPostBack) {

10.4. TRepeater

$this->Repeater->dataBind(); }

} }

Normally, you only need to do this when the page containing the repeater is initially requested. In postbacks, TRepeater is smart enough to remember the previous state, i.e., contents populated with datasource information.The following sample displays tabular data using TRepeater. TRepeater provides several events to facilitate manipulation of its items,

• OnItemCreated - raised each time an item is newly created. When the event is raised, data and child controls are both available for the new item.

• OnItemDataBound - raised each time an item just completes databinding. When the event is raised, data and child controls are both available for the item, and the item has finished databindings of itself and all its child controls.

• OnItemCommand - raised when a child control of some item (such as a TButton) raises an OnCommand event.

The following example shows how to use TRepeater to display tabular data.

Controls.Samples.TRepeater.Sample1 Demo

TRepeater can be used in more complex situations. As an example, we show in the following how to use nested repeaters, i.e., repeater in repeater. This is commonly seen in presenting master- detail data. To use a repeater within another repeater, for an item for the outer repeater is created, we need to set the detail data source for the inner repeater. This can be achieved by responding to the OnItemDataBound event of the outer repeater. An OnItemDataBound event is raised each time an outer repeater item completes databinding. In the following example, we exploit another event of repeater called OnItemCreated, which is raised each time a repeater item (and its content) is newly created. We respond to this event by setting different background colors for repeater items to achieve alternating item background display. This saves us from writing an AlternatingItemTemplate for the repeaters.

Controls.Samples.TRepeater.Sample2 Demo

Besides displaying data, TRepeater can also be used to collect data from users. Validation controls can be placed in TRepeater templates to verify that user inputs are valid.

ThePRADO component composer demo is a good example of such usage. It uses a repeater to collect the component property and event definitions. Users can also delete or adjust the order of the properties and events, which is implemented by responding to the OnItemCommand event of repeater.

See in the following yet another example showing how to use repeater to collect user inputs.

Chapter 11

Control Reference : Active

Controls (AJAX)

11.1

TActiveButton

System.Web.UI.ActiveControls.TActiveButton API Reference

TActiveButton is the active control counter part toTButton. When a TActiveButton is clicked, rather than a normal post back request a callback request is initiated. The OnCallback event is raised during a callback request and it is raise after the OnClick event.

When the ActiveControl.EnableUpdate property is true, changing the Text property during a callback request will update the button’s caption on the client-side.

Since the OnCallback event is raised only during a callback request, the OnCallback event handler can be used to handle logic specifically related to callback requests. The OnClick event handler is raised when ever the button is clicked, even if javascript is disabled.

The following example the use of both the OnClick and OnCallback events of an TActiveButton.

11.1.1

TActiveButton Class Diagram

The class diagram for TActiveButton is illustrated in the figure below. Most active control that can perform callback request have a similar structure.

TActiveButton is an extension ofTButtonand implements two additional interfaces ICallbackEventHandler and IActiveControl. The TActiveButton contains an instance ofTBaseActiveCallbackControl

available through the ActiveControl property of TActiveButton. The following example set the callback parameter of the TActiveButton when a callback request is dispatched.

<com:TActiveButton Text="Click Me"

OnCallback="button_callback"

ActiveControl.CallbackParameter="value" />

In the OnCallback event handler method, the CallbackParameter is available in the $param object.

public function button_callback($sender, $param) {

echo $param->CallbackParameter; //outputs "value" }

In document PRADO v3.1 Quickstart Tutorial 1 (Page 138-143)

Related documents