利用Repeater控件显示主-从关系数据表 (转)
<%@ Import Namespace="System.Data" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<!-- 父Repeater开始 -->
<asp:repeater id="parentRepeater" runat="server">
<itemtemplate>
<hr>
<b>
<%# DataBinder.Eval(Container.DataItem,"au_id") %>
</b>
<br>
<!-- 子Repeater开始 -->
<asp:repeater id="childRepeater" runat="server" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>'>
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "[\"title_id\"]")%>
<br>
</itemtemplate>
</asp:repeater>
<!-- 子Repeater结束 -->
</itemtemplate>
</asp:repeater>
<!-- 父Repeater结束 -->
</form>
</body>
</HTML>
----------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class NestedRepeater : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater parentRepeater;
private void Page_Load(object sender, System.EventArgs e)
{
// 为Authors表创建 Connection 和 DataAdapter
string cnnString = @"server=(local);database=pubs; uid=sa;pwd=sa;";
SqlConnection cnn = new SqlConnection(cnnString);
SqlDataAdapter cmd1 = new SqlDataAdapter("select * from authors",cnn);
DataSet ds = new DataSet();
cmd1.Fill(ds,"authors");
SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titleauthor",cnn);
cmd2.Fill(ds,"titles");
ds.Relations.Add("myrelation",
ds.Tables["authors"].Columns["au_id"],
ds.Tables["titles"].Columns["au_id"]);
parentRepeater.DataSource = ds.Tables["authors"];
parentRepeater.DataBind();
cnn.Dispose();
/*
use pubs
where a.au_id=b.au_id
order by 1
*/
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}