Method 1:
This article mainly explains the issue of Repeater control nested binding in Asp.net. Generally, we use two repeater controls nested binding to bind information under categories and categories. In a general website, user controls of browsing categories are usually located on the left of most ASP. NET pages, which enables users to quickly search for products by category. Recently, I met a customer who asked to add products based on the original category browsing because there are not many products displayed on his website. In addition, the length of the left navigation bar is extended to make the page more harmonious. The original category navigation bar is implemented by repeater. Now we need to add the product information of this category under each category, so I thought of embedding repeater in the original repeater. The implementation interface is as follows: foreground page: Background code (partial code): // when you bind a category name, the product category name rptcategories bound to the category is omitted, bind normally. The following code is the itemdatabound event of the product category name rptcategories.
Private void rptcategories_itemdatabound (Object sender, system. Web. UI. webcontrols. repeateritemeventargs E)
{
Repeater rptproduct = (repeater) E. Item. findcontrol ("rptproduct"); // locate the product category number
Object categorieid = databinder. eval (E. Item. dataitem, "ID"); // query the products under this category based on the category ID, and bind the product
Repeater rptproduct. datasource = method for obtaining the product list (parameter: Convert. tostring (categorieid ));
Rptproduct. databind ();
}
Method 2:
Automatically obtain the master-slave relationship data in the database. The following is the city for obtaining the province and province. The Code is as follows:
<Asp: repeater id = "repeater1" runat = "server">
<Itemtemplate>
<Span style = "font-weight: bold"> <% # eval ("pname") %> </span>
<Ul>
<Asp: repeater id = "detal" runat = "server" datasource = '<% # getcity (eval ("PID"). tostring () %>'>
<Itemtemplate>
<Li> <% # eval ("city_name") %> </LI>
</Itemtemplate>
</ASP: repeater>
</Ul>
</Itemtemplate>
</ASP: repeater>
Important: datasource = '<% # getawd (eval ("PID"). tostring () %>' Call the getcity method to obtain the subclass.
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
Bindgrid ();
}
// Obtain the category of a category
Protected void bindgrid ()
{
String plain text = "select PID, pname from Pro ";
Datatable dt = VC. getdatatable (plain text );
Repeater1.datasource = DT;
Repeater1.databind ();
}
// Obtain the small class content
Protected datatable getcity (string ID)
{
String shorttext = "select city_name from city where promo_id =" + ID + "";
Datatable dt = VC. getdatatable (plain text );
Return DT;
}