The Advanced Repeater web form takes this a few steps further. Instead of just
binding a SqlDataReader to a Repeater control, the Advanced Repeater web form hooks into the
ItemCreated and ItemDataBound events of our Repeater control to dynamically alter its output.
The Advanced Repeater web form dynamically adds an ASP.NET Label control to each
RepeaterItem row in its ItemCreated handler:
protected void repeaterRdrCust_ItemCreated(object o,
ControlsBook2Lib.Ch07.RepeaterItemEventArgs rie)
{
ControlsBook2Lib.Ch07.RepeaterItem item = rie.Item;
if (item.ItemType == ListItemType.Item)
{
Label lblID = new Label();
lblID.ID = "lblID";
item.Controls.Add(lblID);
item.Controls.Add(new LiteralControl(" "));
}
CHAPTER 7 ?– SERVER CONTROL DATA B INDING 319
Once the control data binds, it changes the value of the added Label control to the
CustomerID value of the current row in the SqlDataReader:
protected void repeaterRdrCust_ItemDataBound(object o,
ControlsBook2Lib.Ch07.RepeaterItemEventArgs rie)
{
ControlsBook2Lib.Ch07.RepeaterItem item = rie.Item;
DbDataRecord row = (DbDataRecord)item.DataItem;
string ID = (string)row["CustomerID"];
Label lblID = (Label)item.FindControl("lblID");
lblID.Text = ID;
}
The result of the event handling during creation and data binding is the browser output
shown in Figure 7-7.
Pages:
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433