Function: a repeater is nested in a datalist. Values in the repeater display different data based on the data in the datalist.
Suppose there is a data table:
Now datalist is used to display fathertype and repeater is used to display childtype Based on fathertype. The effect is as follows:
Front-end code:
<Ul>
<Asp: datalist id = "datalist4" runat = "server" width = "204px"
Onitemdatabound = "datalist4_itemdatabound" datakeyfield = "fathertype">
<! -- Note datakeyield here -->
<Headertemplate>
<P> classified browsing </P>
</Headertemplate>
<Itemtemplate>
<Li> <a href = "fengxingbookstore. aspx? Id = <% # eval ("fathertype") %> ">
<Asp: Label id = "label1" runat = "server" text = '<% # eval ("fathertype") %>'> </ASP: Label> </a>
</LI> <br/>
<Asp: repeater id = "repeater1" runat = "server">
<Headertemplate> <HR/> <Itemtemplate>
<A href = "fengxingbookstore. aspx? Id = <% # eval ("childtype") %> ">
<Asp: Label id = "label3" runat = "server" text = '<% # eval ("childtype") %>'> </ASP: Label> & nbsp; & nbsp; </a>
</Itemtemplate>
</ASP: repeater>
</Itemtemplate>
</ASP: datalist>
</Ul>
<Asp: sqldatasource id = "sqldatasource5" runat = "server"
Connectionstring = "<% $ connectionstrings: connectionstring %>"
Selectcommand = "select [childtype] from [goodsclass]"> </ASP: sqldatasource>
<Asp: sqldatasource id = "sqldatasource4" runat = "server"
Connectionstring = "<% $ connectionstrings: connectionstring %>"
Selectcommand = "select distinct fathertype from goodsclass"> </ASP: sqldatasource>
Background code:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Data;
Public partial class mainbooks: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
This. datalist4.datasource = This. sqlperformance4;
This. datalist4.databind ();
}
}
Protected void datalist4_itemdatabound (Object sender, datalistitemeventargs E)
{
If (E. Item. itemtype = listitemtype. Item |
E. Item. itemtype = listitemtype. alternatingitem)
{
String categoryid = This. datalist4.datakeys [E. Item. itemindex]. tostring (); // obtain the value of the current cell
This. sqldatasource5.selectcommand = string. Format ("select childtype from [goodsclass] Where [fathertype] = '{0}'", categoryid );
Repeater repeater1 = (repeater) E. Item. findcontrol ("repeater1 ");
Repeater1.datasource = This. sqlperformance5;
Repeater1.databind (); // be sure to bind dynamically
}
} // If not dynamically bound, all the final data is displayed.
}