本文介绍了一种利用Repeater控件显示主-从关系数据表的方法。
原文参见:http://support.microsoft.com/default.aspx?scid=kb;en-us;306154
NestedRepeater.aspx
<%
@ Import Namespace="System.Data"
%>
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Page language="c#" Codebehind="NestedRepeater.aspx.cs"
AutoEventWireup="false" Inherits="eMeng.NestedRepeater"
%>
<
HTML
>
<
body
>
<
form
runat
="server"
>
<!--
父Repeater开始
-->
<
asp:repeater
id
="parentRepeater"
runat
="server"
>
<
itemtemplate
>
<
b
>
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
# DataBinder.Eval(Container.DataItem,"au_id")
%>
</
b
>
<
br
>
<!--
子Repeater开始
-->
<
asp:repeater
id
="childRepeater"
runat
="server"
datasource
='<%#
((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %
>
'>
<
itemtemplate
>
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
# DataBinder.Eval(Container.DataItem, "[\"title_id\"]")
%>
<
br
>
</
itemtemplate
>
</
asp:repeater
>
<!--
子Repeater结束
-->
</
itemtemplate
>
</
asp:repeater
>
<!--
父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;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
eMeng
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
/// NestedRepeater 的摘要说明。
/// 本文介绍如何利用Repeater控件显示主-从关系的表格
/// 原文参见:
/// http://support.microsoft.com/default.aspx?scid=kb;en-us;306154
///
public class NestedRepeater : System.Web.UI.Page
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
protected System.Web.UI.WebControls.Repeater parentRepeater;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Page_Load(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
// 为Authors表创建 Connection 和 DataAdapter
string cnnString = @"server=(local)\NetSDK;database=pubs; Integrated Security=SSPI;";
SqlConnection cnn = new SqlConnection(cnnString);
SqlDataAdapter cmd1 = new SqlDataAdapter("select * from authors",cnn);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//创建填充 DataSet.
DataSet ds = new DataSet();
cmd1.Fill(ds,"authors");
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 为Titles表创建 DataAdapter
SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titleauthor",cnn);
cmd2.Fill(ds,"titles");
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 创建 Authors 表和 Titles 表之间的关系.
ds.Relations.Add("myrelation",
ds.Tables["authors"].Columns["au_id"],
ds.Tables["titles"].Columns["au_id"]);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 绑定Authors到父Repeater
parentRepeater.DataSource = ds.Tables["authors"];
Page.DataBind();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cnn.Close();
cnn.Dispose();
}
private void Page_Init(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
InitializeComponent();
}
private void InitializeComponent()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
this.Load += new System.EventHandler(this.Page_Load);
}
public NestedRepeater()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
Page.Init += new System.EventHandler(Page_Init);
}
}
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=27695