Nested repeater is quite simple and clever

Source: Internet
Author: User
Repeater nesting method, which is quite simple

First, the ASPX page

<Asp: repeater id = "RP" runat = "server">
<Itemtemplate>
<% # Eval ("ID") %>
<Asp: repeater id = "RP1" runat = "server" datasource = '<% # getdata (eval ("ID") %>'>
<Itemtemplate>
<% # Eval ("name") %> <br/>
</Itemtemplate>
</ASP: repeater>
<Br/>
</Itemtemplate>
</ASP: repeater>

The key is <asp: repeater id = "RP1" runat = "server" datasource = '<% # getdata (eval ("ID") %>'>
Getdata is a UDF with the following prototype:

Protected datatable getdata (Object OBJ)
{
Datatable dt = new datatable ();
// Do something here
Return DT;
}

The complete code is as follows: aspx

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "test. aspx. cs" inherits = "test" %>
<% @ Import namespace = "system. Data" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/5o/xhtml">
<Head runat = "server">
<Title> NID's blog http://www.ljnid.cn </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: repeater id = "RP" runat = "server">
<Itemtemplate>
<% # Eval ("ID") %>
<Asp: repeater id = "RP1" runat = "server" datasource = '<% # getdata (eval ("ID") %>'>
<Itemtemplate>
<% # Eval ("name") %> <br/>
</Itemtemplate>
</ASP: repeater>
<Br/>
</Itemtemplate>
</ASP: repeater>
</Div>
</Form>
</Body>
</Html>

Aspx. CS File

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Public partial class test: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Datatable dt = new datatable ();
DT. Columns. Add ("ID ");
For (INT I = 0; I <4; I ++)
{
Datarow DR = DT. newrow ();
Dr ["ID"] = I;
DT. Rows. Add (DR );
}

RP. datasource = DT;
RP. databind ();
}
Protected datatable getdata (Object OBJ)
{
Datatable dt = new datatable ();
DT. Columns. Add ("name ");
For (INT I = 0; I <10; I ++)
{
Datarow DR = DT. newrow ();
Dr ["name"] = "name _" + OBJ + "_" + I;
DT. Rows. Add (DR );
}
Return DT;
}
}

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.