TRepeater tutorial
From PRADO Wiki
The purpose
TRepeater is very simple at first look. It's pratically foreach wrapped as PRADO component. Well, it has much more capabilities and catches than usual foreach . Perhaps this article will become a place for these tips and tricks.
Component inside TRepeater and its events
Let's say we have this code:
<com:TRepeater ID="Repeater"> <prop:ItemTemplate> <com:TTextBox OnTextChanged="Page.TextChanged" Text=<%# $this->DataItem['text'] %> /> <br /> </prop:ItemTemplate> </com:TRepeater> <com:TButton OnClick="Page.Submit" Text="Submit" />
As we can see it's simple case and self explanatory. Now the trick is that after Repeater is databound it's datasource is cleared so Page.TextChanged have no idea what DataItem TRepeaterItem had.
Qiang explained that is done for perfomance reasons. There are two solutions out of this situation.
- Save the datasource in page viewstate/controlstate.
- Use DataKeyField of TRepeater:
Let's say we have such datasource:
$DataSource = array( array('id' => 0, 'text' => 'text0'), array('id' => 1, 'text' => 'text1'), array('id' => 2, 'text' => 'text2'), );
Let's add DataKeyField="id" to TRepeaters properties. This stores one field, id, in TRepeater by this association: TRepeater->DataKeys[ TRepeaterItemIndex ] = TRepeaterItem->DataItem['id'].
Then in PHP code we can write our TextChanged handler like this:
public function TextChanged($sender, $param) { // Our $sender is TTextBox and its naming container is TRepeaterItem. $item = $sender->NamingContainer; // TRepeaterItem has TRepeater as its parent. $repeater = $item->Parent; // Get item index in TRepeater. $itemIndex = $repeater->Items->indexOf($item); // Finally get the id value of that TRepeaterItem's DataItem. $id = $repeater->DataKeys[ $itemIndex ]; // Now we can update the database by grabbed ID. $db = getDB(); $db->query( 'UPDATE `text_table` SET `text`=? WHERE `id`=?', array($sender->getText(), $key) ); }

