A sample introduction to nested repeater in repeater _ Practical Tips

Source: Internet
Author: User
Tags prepare
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.