Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
if (! Page.IsPostBack)
{
This.dtcategory = Getcategorytable ();
This.dtproduct = Getproducttable ();
Rptcategorylist.datasource = dtcategory;
Rptcategorylist.databind ();
}
}
Prepare a list of categories
DataTable getcategorytable ()
{
DataTable dt = new DataTable ();
Dt. Columns.Add ("CategoryID", typeof (int));
Dt. Columns.Add ("Categorytitle", typeof (String));
for (int i = 1; I <= 3; i++)
{
DataRow row = dt. NewRow ();
Row["CategoryID"] = i;
row["Categorytitle"] = "Classification name" + i + "";
Dt. Rows.Add (row);
}
return DT;
}
Prepare a list of products
DataTable getproducttable ()
{
DataTable dt = new DataTable ();
Dt. Columns.Add ("ProductTitle", typeof (String));
Dt. Columns.Add ("CategoryID", typeof (int));
for (int i = 1; I <= 9; i++)
{
DataRow row = dt. NewRow ();
row["ProductTitle"] = "Product Name" + i + "";
if (i > 6) row["CategoryID"] = 3;
else if (i > 3) row["CategoryID"] = 2;
else row["CategoryID"] = 1;
Dt. Rows.Add (row);
}
return DT;
}
Get a product of a category
DataTable getproducttable (int CategoryID)
{
DataView dv = This.dtProduct.DefaultView;
Dv. RowFilter = "Categoryid=" + CategoryID + "";
Return DV. ToTable ();
}
protected void Rptcategorylist_itemdatabound (object sender, RepeaterItemEventArgs e)
{
if (E.item.itemtype = = ListItemType.AlternatingItem | | e.item.itemtype = = listitemtype.item)
{
DataRowView DRV = (DataRowView) e.Item.DataItem;
Literal Ltltitle = (Literal) e.item.findcontrol ("Ltltitle");
Ltltitle.text = drv["Categorytitle"]. ToString ();
Repeater rptproductlist = (Repeater) e.item.findcontrol ("Rptproductlist");
Rptproductlist.datasource = getproducttable (Convert.ToInt32 (drv["CategoryID"));
Rptproductlist.databind ();
}
}
protected void Rptproductlist_itemdatabound (object sender, RepeaterItemEventArgs e)
{
if (E.item.itemtype = = ListItemType.AlternatingItem | | e.item.itemtype = = listitemtype.item)
{
DataRowView DRV = (DataRowView) e.Item.DataItem;
Literal Ltltitle = (Literal) e.item.findcontrol ("Ltltitle");
Ltltitle.text = drv["ProductTitle"]. ToString ();
}
}
Foreground aspx code
Copy Code code as follows:
<</code>form id= "Form1" runat= "Server" >
<</CODE>div>
<</code>asp:repeater id= "rptcategorylist" runat= "Server" onitemdatabound= "Rptcategorylist_itemdatabound" >
<</CODE>ItemTemplate>
<</code>div class= "ListBox" >
<</code>div class= "title" >
<</code>asp:literal id= "Ltltitle" runat= "Server" ></</CODE>asp:Literal></</CODE> Div>
<</code>div class= "Content" >
<</CODE>ul>
<</code>asp:repeater id= "rptproductlist" runat= "Server" onitemdatabound= "Rptproductlist_itemdatabound" >
<</CODE>ItemTemplate>
<</CODE>li>
<</code>asp:literal id= "Ltltitle" runat= "Server" ></</CODE>asp:Literal>
</</CODE>li>
</</CODE>ItemTemplate>
</</CODE>asp:Repeater>
</</CODE>ul>
</</CODE>div>
</</CODE>div>
</</CODE>ItemTemplate>
</</CODE>asp:Repeat</</CODE>div>
</</CODE>form>