常常在csdn上看到有网友问如何才能实现repeater,datalist, datagrid的嵌套问题,下面给出一个3层嵌套的示例,可以无限级嵌套下去
<asp:repeater id=”rpt_catalog” onitemdatabound=”rpt_catalog_onitemdatabound” runat=”server”>
<itemtemplate>最顶层repeater,index:<%#container.dataitem%><br>
<asp:repeater id=”rpt_board” onitemdatabound=”rpt_board_onitemdatabound” runat=”server”>
<itemtemplate> 第2层repeater,index:<%#container.dataitem%><br>
<asp:repeater id=”rpt_boardmaster” runat=”server”>
<itemtemplate> 第3层repeater,index:<%#container.dataitem%><br>
</itemtemplate>
</asp:repeater>
</itemtemplate>
</asp:repeater>
</itemtemplate>
</asp:repeater>
private intfirst as integer
private intsecond as integer
private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
put user code to initialize the page here
dim al as new arraylist
al.add(0)
al.add(1)
rpt_catalog.datasource = al
rpt_catalog.databind()
end sub
sub rpt_catalog_onitemdatabound(byval sender as object, byval e as repeateritemeventargs)
dim rpt as repeater
rpt = e.item.findcontrol(“rpt_board”)
intfirst = e.item.itemindex
dim al as new arraylist
al.add(intfirst & “.0”)
al.add(intfirst & “.1”)
rpt.datasource = al
rpt.databind()
end sub
sub rpt_board_onitemdatabound(byval sender as object, byval e as repeateritemeventargs)
dim rpt as repeater
rpt = e.item.findcontrol(“rpt_boardmaster”)
intsecond = e.item.itemindex
dim al as new arraylist
al.add(intfirst & “.” & intsecond & “.0”)
al.add(intfirst & “.” & intsecond & “.1”)
rpt.datasource = al
rpt.databind()
end sub
代码经过测试,但请自行优化