Front-end page:
< ASP: Repeater ID = "Rptcategories" Runat = "Server" >
< Headertemplate >
< Table Width = "100%" Border = "0" Cellspacing = "0" Cellpadding = "0" >
</ Headertemplate >
< Itemtemplate >
<! -- Category name -->
< Tr > < Th > <% # Databinder.Eval(Container. dataitem,"Typename") %> </ Th > </ Tr >
<! -- Products under category -->
< ASP: Repeater ID = "Rptproduct" Runat = "Server" >
< Itemtemplate >
< Tr > < TD > < A Href = 'Productinfo. aspx? Id = <% # Databinder. eval (container. dataitem, "ID") % > '> <% # Databinder.Eval(Container. dataitem,"Productname") %> </ A > </ TD > </ Tr >
</ Itemtemplate >
</ ASP: Repeater >
</ Itemtemplate >
< Footertemplate >
</ Table >
</ Footertemplate >
</ ASP: Repeater >
BackgroundCodePart (part of code ):
// When you bind a category name to a product under the category
Private Void Rptcategories_itemdatabound ( Object Sender, system. Web. UI. webcontrols. repeateritemeventargs E)
{
Bll. Products = New Bll. Products ();
If (E. Item. itemtype = Listitemtype. Item | E. Item. itemtype = Listitemtype. alternatingitem)
{
Repeater rptproduct = (Repeater) E. Item. findcontrol ( " Rptproduct " );
// Locate the data item associated with the category Repeater
Datarowview rowv = (Datarowview) E. Item. dataitem;
// Extract category ID
Int Categorieid = Convert. toint32 (rowv [ " ID " ]);
// Query the products under the category based on the category ID, and bind the product Repeater
Rptproduct. datasource = Products. getproductsbycategorieid (categorieid );
Rptproduct. databind ();
}
}
Another method:
Front-end:
<! -- Start parent repeater -->
<Asp: repeater id = "parentrepeater" runat = "server">
<Itemtemplate>
<B> <% # databinder. eval (container. dataitem, "au_id") %> </B> <br>
<! -- Start child repeater -->
<Asp: repeater id = "childrepeater" datasource = '<% # (datarowview) container. dataitem)
. Row. getchildrows ("myrelation") %> 'runat = "server">
<Itemtemplate>
<% # Databinder. eval (container. dataitem, "[\" title_id \ "]") %> <br>
</Itemtemplate>
</ASP: repeater>
<! -- End Child repeater -->
</Itemtemplate>
</ASP: repeater>
<! -- End parent repeater -->
Background: public partial class nestedrepeater: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. Repeater childrepeater; // parentrepeater;
Public nestedrepeater ()
{
Page. init + = new system. eventhandler (page_init );
}
Public void page_load (Object sender, eventargs E)
{
// Create the connection and dataadapter for the authors table.
Sqlconnection CNN = new sqlconnection ("Server =.; database = pubs; user id = sa; Pwd = ;");
Sqldataadapter cmd1 = new sqldataadapter ("select * from authors", CNN );
// Create and fill the dataset.
Dataset DS = new dataset ();
Statement 1.fill (DS, "Authors ");
// Create a second dataadapter for the titles table.
Sqldataadapter cmd2 = new sqldataadapter ("select * From titleauthor", CNN );
Listen 2.fill (DS, "titles ");
// Create the relation bewtween the authors and titles tables.
DS. relations. Add ("myrelation ",
DS. Tables ["Authors"]. Columns ["au_id"],
DS. Tables ["titles"]. Columns ["au_id"]);
// bind the authors table to the parent Repeater control, and call databind.
parentrepeater. datasource = Ds. tables ["Authors"];
page. databind ();
// Close the connection.
CNN. Close ();
}
Private void page_init (Object sender, eventargs E)
{
Initializecomponent ();
}
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}
}