|
nestedrepeater.aspx <%@ page language=c# inherits="yourprojectname.nestedrepeater" %> <%@ import namespace="system.data" %>
<html> <body> <form runat=server>
<!-- start parent repeater --> <asp:repeater id="parent" runat="server"> <itemtemplate> <b><%# databinder.eval(container.dataitem,"au_id") %></b><br>
<!-- start child repeater --> <asp:repeater id="child" 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 -->
</form> </body> </html> nestedrepeater.aspx.cs using system; using system.data; using system.data.sqlclient; using system.web; using system.web.sessionstate; using system.web.ui; using system.web.ui.webcontrols;
namespace yourprojectname { public class nestedrepeater : system.web.ui.page { protected system.web.ui.webcontrols.repeater parent; 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=(local);database=pubs;uid=sa;pwd=;"); sqldataadapter cmd1 = new sqldataadapter("select * from authors",cnn);
//create and fill the dataset. dataset ds = new dataset(); cmd1.fill(ds,"authors");
//create a second dataadapter for the titles table. sqldataadapter cmd2 = new sqldataadapter("select * from titleauthor",cnn); cmd2.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. parent.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); } } } |