Datalist nested repeater display

Source: Internet
Author: User

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.
}

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.