<asp:Repeater ID="myrpList" runat="server">
<HeaderTemplate>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td width="4%" height="25" valign="top"> </td>
<td width="96%" valign="top"><strong><%# Eval("classname")%></strong></td>
</tr>
<tr>
<td height="25" valign="top"> </td>
<td valign="top">
<asp:DataList ID="mydList" runat="server"><HeaderTemplate>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td> <%# Eval("title")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table></FooterTemplate></asp:DataList></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<HeaderTemplate>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td width="4%" height="25" valign="top"> </td>
<td width="96%" valign="top"><strong><%# Eval("classname")%></strong></td>
</tr>
<tr>
<td height="25" valign="top"> </td>
<td valign="top">
<asp:DataList ID="mydList" runat="server"><HeaderTemplate>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td> <%# Eval("title")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table></FooterTemplate></asp:DataList></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
CS:
void BinData()
{
WebClass.ArticleList tmp = new WebClass.ArticleList();
DataTable dt = tmp.ListClass();
this.myrpList.DataSource = dt;
this.myrpList.DataBind();
int strID;
foreach (RepeaterItem it in this.myrpList.Items)
{
int i = int.Parse(it.ItemIndex.ToString());
strID = int.Parse(dt.Rows[i]["id"].ToString());
DataList dlist = (DataList)it.FindControl("mydList");
dlist.DataSource = tmp.ShowClassAricle(strID);
dlist.DataBind();
}
}
{
WebClass.ArticleList tmp = new WebClass.ArticleList();
DataTable dt = tmp.ListClass();
this.myrpList.DataSource = dt;
this.myrpList.DataBind();
int strID;
foreach (RepeaterItem it in this.myrpList.Items)
{
int i = int.Parse(it.ItemIndex.ToString());
strID = int.Parse(dt.Rows[i]["id"].ToString());
DataList dlist = (DataList)it.FindControl("mydList");
dlist.DataSource = tmp.ShowClassAricle(strID);
dlist.DataBind();
}
}
网上看到的:
//在绑定分类品名时,绑定分类下的产品
private void rptCategories_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
BLL.Products products =new BLL.Products();
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rptProduct = (Repeater) e.Item.FindControl("rptProduct");
//找到分类Repeater关联的数据项
DataRowView rowv = (DataRowView)e.Item.DataItem;
//提取分类ID
int CategorieId = Convert.ToInt32(rowv["ID"]);
//根据分类ID查询该分类下的产品,并绑定产品Repeater
rptProduct.DataSource = products.GetProductsByCategorieId(CategorieId);
rptProduct.DataBind();
}
}
private void rptCategories_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
BLL.Products products =new BLL.Products();
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rptProduct = (Repeater) e.Item.FindControl("rptProduct");
//找到分类Repeater关联的数据项
DataRowView rowv = (DataRowView)e.Item.DataItem;
//提取分类ID
int CategorieId = Convert.ToInt32(rowv["ID"]);
//根据分类ID查询该分类下的产品,并绑定产品Repeater
rptProduct.DataSource = products.GetProductsByCategorieId(CategorieId);
rptProduct.DataBind();
}
}
还有一种方法:
<!-- 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 -->
<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 -->
cs:
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();
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.
parentRepeater.DataSource = ds.Tables["authors"];
Page.DataBind();
//Close the connection.
cnn.Close();
}
{
//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();
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.
parentRepeater.DataSource = ds.Tables["authors"];
Page.DataBind();
//Close the connection.
cnn.Close();
}