我想使用嵌套转发器添加多个category->subcategory-> sub-subcategory
:
http://i.stack.imgur.com/bMiYZ.png
与此相似,但数据库中包含更多类别和子类别。
<form id="form1" runat="server">
<div>
<ul>
<asp:Repeater ID="outerRep" runat="server" OnItemDataBound="outerRep_ItemDataBound">
<ItemTemplate>
<li>
<asp:Label Font-Size="Large" Font-Bold="true" ID="lblCategoryName" runat="server" Text='<%# Eval("CategoryName") %>' />
</li>
<ul>
<asp:Repeater ID="innerRep" runat="server">
<ItemTemplate>
<li style="background-color: AliceBlue">
<asp:HyperLink ID="hlProductName" runat="server" Text='<%# Eval("SubCategoryName")%>' />
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</ItemTemplate>
</asp:Repeater>
</ul>
</div>
</form>
代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
private void BindData()
{
SqlConnection myConnection = new SqlConnection("Data Source=.; uid=sa; pwd=xxx;database=registration;");
SqlCommand myCommand = new SqlCommand("usp_GetProductsForCategories", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
SqlDataAdapter ad = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
ad.Fill(ds);
// Attach the relationship to the dataSet
ds.Relations.Add(new DataRelation(
"CategoriesRelation",
ds.Tables[0].Columns["CategoryID"],
ds.Tables[1].Columns["CategoryID"]));
outerRep.DataSource = ds.Tables[0];
outerRep.DataBind();
}
protected void outerRep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv = e.Item.DataItem as DataRowView;
Repeater innerRep = e.Item.FindControl("innerRep") as Repeater;
innerRep.DataSource = drv.CreateChildView("CategoriesRelation");
innerRep.DataBind();
}
}
还是有更好的方法吗?请帮忙。