使用嵌套 Repeater 控件和 Visual C# .Net 显示分层数据

出自微软的官方Repeater嵌套实例。此例简单易懂,适合初步学习和扩展试验。

完整代码列表

<script type="text/javascript">loadTOCNode(2, 'summary');</script>
Nestedrepeater.aspx

<%@ Page language="c#" Codebehind="NestedRepeater.aspx.cs" AutoEventWireup="false" Inherits="NestedRepeater.NestedRepeater" %> <%@ Import Namespace="System.Data" %> <html> <body> <form runat=server> <!-- start parent repeater --> <asp:repeater id="parentRepeater" runat="server"> <itemtemplate> <b><%# DataBinder.Eval(Container.DataItem,"au_id") %></b><br> <!-- start child repeater --> <asp:repeater id="childRepeater" datasource='<%# ((DataRowView)Container.DataItem) .Row.GetChildRows("myrelation") %>' runat="server"> <itemtemplate> <%# DataBinder.Eval(Container.DataItem, "[/"title_id/"]")%><br> </itemtemplate> </asp:repeater> <!-- end child repeater --> </itemtemplate> </asp:repeater> <!-- end parent repeater --> </form> </body> </html>

Nestedrepeater.aspx.cs

using System; using System.Data; using System.Data.SqlClient; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; namespace NestedRepeater { public class nestedrepeater :System.Web.UI.Page    { protected System.Web.UI.WebControls.Repeater parentRepeater; public NestedRepeater()       { Page.Init += new System.EventHandler(Page_Init);       } public void Page_Load(object sender, EventArgs e)       { //Create the connection and DataAdapter for the Authors table. SqlConnection cnn = new SqlConnection("server=(local);database=pubs; Integrated Security=SSPI ;"); SqlDataAdapter cmd1 = new SqlDataAdapter("select * from authors",cnn); //Create and fill the DataSet. DataSet ds = new DataSet(); cmd1.Fill(ds,"authors"); //Create a second DataAdapter for the Titles table. SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titleauthor",cnn); cmd2.Fill(ds,"titles"); //Create the relation bewtween the Authors and Titles tables. ds.Relations.Add("myrelation", ds.Tables["authors"].Columns["au_id"], ds.Tables["titles"].Columns["au_id"]); //Bind the Authors table to the parent Repeater control, and call DataBind. parentRepeater.DataSource = ds.Tables["authors"]; Page.DataBind(); //Close the connection. cnn.Close();       } private void Page_Init(object sender, EventArgs e)       { InitializeComponent();       } private void InitializeComponent()       {    this.Load += new System.EventHandler(this.Page_Load);       }    } }

<script type="text/javascript">loadTOCNode(3, 'summary');</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值