本文提供如何使用巢狀重複項控件和 Visual C# .NET 顯示階層式數據的相關信息。
原始產品版本: Visual C#
原始 KB 編號: 306154
摘要
本文說明如何使用巢狀重複項控件來顯示階層式數據。 您可以將這個概念套用至其他清單綁定控件。
本文參考下列Microsoft .NET Framework 類別庫命名空間:
System.DataSystem.Data.SqlClient
系結至父數據表
啟動 Visual Studio .NET。
在 [檔案] 功能表上,指向 [開新檔案] ,然後按一下 [專案] 。
按兩下 [項目類型] 下的 [Visual C# 專案],然後按兩下 [範本] 下的 [ASP.NET Web 應用程式]。
在 [ 位置] 方塊中 ,刪除 WebApplication#,然後輸入 NestedRepeater。 如果您使用本地伺服器,請將伺服器名稱保留為
http://localhost。 路徑http://localhost/NestedRepeater會出現在 [ 位置] 方塊中。 按一下 [確定]。在 方案總管 中,以滑鼠右鍵按兩下NestedRepeater專案名稱節點,指向[新增],然後按兩下 [新增Web表單]。
若要命名 Web 窗體,請輸入 NestedRepeater,然後按兩下 [ 開啟]。
會建立新的 Web 窗體。 它會在 Visual Studio .NET 集成開發環境 (IDE) 的設計檢視中開啟。 從 [工具箱] 中,選取 [重複程式] 控件,然後將它拖曳至 [網頁窗體] 頁面。
將此 Repeater 控制件的 ID 屬性變更為 parentRepeater。
切換至此網頁表單的 HTML 檢視。 若要這樣做,請按兩下 設計工具左下角的 HTML 索引標籤。 Repeater 控制項會產生下列 HTML 程式代碼:
<asp:Repeater id="parentRepeater" runat="server"></asp:Repeater>在標記新增
Repeater下列程式代碼:<itemtemplate> <b> <%# DataBinder.Eval(Container.DataItem, "au_id") %> </b> <br> </itemtemplate>執行此動作之後,Repeater 的 HTML 程式代碼如下所示:
<asp:Repeater id="parentRepeater" runat="server"> <itemtemplate> <b> <%# DataBinder.Eval(Container.DataItem, "au_id") %> </b> <br> </itemtemplate> </asp:Repeater>在 方案總管 中,以滑鼠右鍵按兩下 [NestedRepeater.aspx],然後按兩下 [檢視程式代碼] 切換至NestedRepeater.aspx.cs程序代碼後置檔案。
將下列命名空間宣告新增至檔案頂端:
using System.Data; using System.Data.SqlClient;將下列程式代碼新增至
Page_Load事件,以建立 Pubs 資料庫的連線,然後將數據表系結Authors至 Repeater 控制項: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(); }注意
您可能必須視環境適當地修改資料庫 連接字串。
儲存所有檔案。
在 方案總管 中,以滑鼠右鍵按兩下NestedRepeater.aspx,然後按兩下 [設定為起始頁]。
在 [ 建置] 功能表上,按兩下 [建置方案 ] 來編譯專案。
在瀏覽器中檢視.aspx頁面,然後確認頁面到目前為止是否正常運作。 輸出應該如下所示:
172-32-1176 213-46-8915 238-95-7766 267-41-2394 ...
系結至子數據表
在 [NestedRepeater.aspx] 頁面的 HTML 檢視中,找出下列程式代碼行:
<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>這個新程式代碼會將第二個 Repeater 控件新增至
ItemTemplate父 Repeater 控件的 屬性。DataSource設定子 Repeater 控件的 屬性,如下所示:<asp:repeater ... datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>' >設定
DataSource子 Repeater 控制件的 屬性之後,兩個 Repeater 控制件的 HTML 程式代碼會顯示如下:<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>將下列頁面指示詞新增至頁面頂端:
<%@ Import Namespace="System.Data" %>在程式代碼後置頁面中,取代 事件中的
Page_Load下列這一行://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"]);這會將
Titles數據表新增至 DataSet,然後加入 和Titles數據表之間的Authors關聯性。儲存並編譯應用程式。
在瀏覽器中檢視頁面,然後確認頁面到目前為止是否正常運作。 輸出應該如下所示:
172-32-1176 PS3333 213-46-8915 BU1032 BU2075 238-95-7766 PC1035 267-41-2394 BU1111 TC7777
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);
}
}
}
其他相關資訊
如需詳細資訊,請參閱 Repeater Web Server Control。