Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_professional thread: Repeater nested in a datalist


Message #1 by "Bob Riley" <briley@c...> on Wed, 19 Feb 2003 04:59:19
Thanks incredibly much!  This will definitely help! 

-----Original Message-----
From: Armando Andrade [mailto:mandrade@t...]
Sent: Wednesday, February 19, 2003 1:36 PM
To: ASPX_Professional
Subject: [aspx_professional] Re: Repeater nested in a datalist


Hi there, im new to the list, and i think i can help you with this, i 
have
a similar problem, i used a datagrid to display movies and inside my row 
i
needed to insert the times of the movie, where every time is a row in 
the
database, so the best way, was to nest a repeater there, so check out 
the
code.

The first thing, your DataList has a source like a DB, XML File, etc...
so it generates all the rows, the trick is to use the ItemDataBound
method, this method excute the code in it, each time a row is bind to 
the
DataList

public void DataList_ItemDataBound(object sender, DataGridItemEventArgs 
e)

this is the declaration of the method

if(e.Item.ItemType =3D=3D ListItemType.Item || e.Item.ItemType =3D=3D
ListItemType.AlternatingItem)

This IF clause, is used to remove the header or footer items of the
datagrid, so you are selecting the items and alternatingitems (in case 
you
declare them).

then it comes to fun stuff, in this example im creating the repetear in
the codebehind (you can do it in the design view too)

Repeater myrep =3D new Repeater();
myrep.ItemTemplate =3D new RepeaterTemplate();

here im using a predefined class, to build the template for my repeater, 

but remember that you can build the template in the design mode.

myrep.DataSource =3D (DataRowView)e.Item.DataItem).CreateChildView
("myRelation");

now here is the magic, i cast my actual row of data that is passed with
the "e" argument, to a DataRowView object, so i can expose a
createchildview method, this one create an array of DataRowView objects
that are the childs of my actual row, so i can bind them to the 
repeater.

e.Item.Cells[1].Controls.Add(myrep);
myrep.DataBind();
then, i add my control to the appropiate cell, and finally i call the
databind method of the repeater, this is because i read that is a good
practice to bind the control, after is added to the cell.

As i told you, you can design your repeater in design mode, and maybe 
you
want to fetch the data to your datasource each time a row is accessed, 
but
remeber that this is a great performance hit, since you'll have to open 
an
close a connection each time a row is accessed, so i recommend you what 
i
did, read all the info you need once, save that in a DataSet, (remember
you can have multiple tables there) then create the relationships you
need, and access the values from there, and if you have a lot of hits to 

that page, consider to cache the DataSet.

I hope these will really help you.

  Return to Index