Repeater双重绑定(子菜单前台代码:)
前台代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<asp:Repeater ID="parentRepeater" runat="server" OnItemDataBound="parentRepeater_ItemDataBound" Visible=false>
<ItemTemplate>
<b>一级:
<%# DataBinder.Eval(Container.DataItem,"categorytitle") %>
</b>
<br>
<!-- 子Repeater开始 -->
<asp:Repeater ID="childRepeater" runat="server">
<ItemTemplate>
二级:
<%# DataBinder.Eval(Container.DataItem, "newstitle")%>
<br>
</ItemTemplate>
</asp:Repeater>
<!-- 子Repeater结束 -->
</ItemTemplate>
</asp:Repeater>
后台代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void Page_Load(object sender, EventArgs e)
{
SqlDataAdapter sda = new SqlDataAdapter("select * from newscategory", con);
DataSet ds = new DataSet();
sda.Fill(ds, "table1");
DataTable dt = new DataTable();
DataView dv = new DataView(ds.Tables["table1"]);
}
protected void parentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater r2 = (Repeater)e.Item.FindControl("childRepeater");
//找到分类Repeater关联的数据项
DataRowView rowv = (DataRowView)(e.Item.DataItem);
//提取分类ID
string CategorieId = Convert.ToString(rowv["categoryid"]);
//根据分类ID查询该分类下的产品,并绑定产品Repeater
string sql = "select distinct top 6 newstitle from newscontent,newscategory where newscontent.categoryid = '" + CategorieId + "'";
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "table2");
r2.DataSource = ds.Tables["table2"].DefaultView;
r2.DataBind();
}
}
技术文档