asp.net repeat嵌套
Bind to the Parent Table
- Start Microsoft Visual Studio .NET.
- On the File menu, point to New, and then click Project.
- Click Visual C# Projects under Project Types, and then click ASP.NET Web Application underTemplates.
- In the Location box, delete the WebApplication#, and then type NestedRepeater. If you use the local server, leave the server name as http://localhost. The following path appears in the Location box:
http://localhost/ NestedRepeaterClick OK.
- In Solution Explorer, right-click the NestedRepeater project name node, point to Add, and then clickAdd Web Form.
- To name the Web Form, type NestedRepeater, and click Open.
- The new Web Form is created. It opens in Design View in the Integrated Development Environment (IDE) of Microsoft Visual Studio .NET. From the Toolbox, select the Repeater control, and then drag it to the Web Form page.
- Change the ID property of this Repeater control to parentRepeater.
- Switch to the HTML view for this Web Form. To do so, click the HTML tab in the lower-left corner of the Designer. The Repeater control generates the following HTML code:
<asp:Repeater id="parentRepeater" runat="server"></asp:Repeater>
- Add the following code in the Repeater tags:
<itemtemplate> <b><%# DataBinder.Eval(Container.DataItem, "au_id") %></b><br> </itemtemplate>
<asp:Repeater id="parentRepeater" runat="server"> <itemtemplate> <b><%# DataBinder.Eval(Container.DataItem, "au_id") %></b><br> </itemtemplate> </asp:Repeater>
- In Solution Explorer, right-click NestedRepeater.aspx, and then click View Code to switch to the NestedRepeater.aspx.cs code-behind file.
- Add the following namespace declaration to the top of the file:
using System.Data; using System.Data.SqlClient;
- Add the following code to the Page_Load event to create a connection to the Pubs database, and then to bind the Authors table to the Repeater control:
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"); //Insert code in step 4 of the next section here. //Bind the Authors table to the parent Repeater control, and call DataBind. parentRepeater.DataSource = ds.Tables["authors"]; Page.DataBind(); //Close the connection. cnn.Close(); }
- Save all of the files.
- In Solution Explorer, right-click the NestedRepeater.aspx, and then click Set As Start Page.
- On the Build menu click Build Solution to compile the project.
- View the .aspx page in the browser, and then verify that the page works thus far.
The output should appear as follows:- 172-32-1176
- 213-46-8915
- 238-95-7766
- 267-41-2394
- ...
Bind to the Child Table
- In the HTML view of the NestedRepeater.aspx page, locate the following line of code:
<b><%# DataBinder.Eval(Container.DataItem, "au_id") %></b><br>
<asp:repeater id="childRepeater" runat="server"> <itemtemplate> <%# DataBinder.Eval(Container.DataItem, "[\"title_id\"]")%><br> </itemtemplate> </asp:repeater>
- Set the DataSource property for the child Repeater control as follows:
<asp:repeater ... datasource='<%# ((DataRowView)Container.DataItem) .Row.GetChildRows("myrelation") %>' >
<asp:Repeater id="parentRepeater" runat="server"> <itemtemplate> <b> <%# DataBinder.Eval(Container.DataItem, "au_id") %> </b> <br> <asp:repeater id="childRepeater" runat="server" datasource='<%# ((DataRowView)Container.DataItem) .Row.GetChildRows("myrelation") %>' > <itemtemplate> <%# DataBinder.Eval(Container.DataItem, "[\"title_id\"]")%><br> </itemtemplate> </asp:Repeater> </itemtemplate> </asp:Repeater>
- Add the following page directive to the top of the page:
<%@ Import Namespace="System.Data" %>
- In the code-behind page, replace the following line in the Page_Load event
//Insert code in step 4 of the next section here.
//Create a second DataAdapter for the Titles table. SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titleauthor",cnn); cmd2.Fill(ds,"titles"); //Create the relation between the Authors and Titles tables. ds.Relations.Add("myrelation", ds.Tables["authors"].Columns["au_id"], ds.Tables["titles"].Columns["au_id"]);
- Save and compile the application.
- View the page in the browser, and then verify that the page works so far. The output should appear as follows:
172-32-1176
PS3333
213-46-8915
BU1032
BU2075
238-95-7766
PC1035
267-41-2394
BU1111
TC7777
...
Complete Code List
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); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
2011-04-06 导出DataGrid数据为Excel让用户下载(转载)