(二)代码
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="数据绑定控件同时显示主表和子表数据.WebForm1" %>
<%@ 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 MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<!-- 绑定主表数据 -->
<asp:repeater id="myRepeater" runat="server">
<HeaderTemplate>
<table border="0" bgcolor="lightblue">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="background-color: lightgray">
<b>
<%# DataBinder.Eval(Container.DataItem,"TypeID") %>
<%# DataBinder.Eval(Container.DataItem,"TypeName") %>
</b>
<br>
<!-- 绑定子表数据 -->
<asp:repeater id="childRepeater" datasource='<%# ((DataRowView)Container.DataItem)
.Row.GetChildRows("TypeRelation") %>' runat="server">
<itemtemplate>
<table border="0" bgcolor="#ffcc33">
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem,"[\"TypeID\"]") %>
<%# DataBinder.Eval(Container.DataItem, "[\"TypeDetail\"]")%>
<br>
</td>
</tr>
</table>
</itemtemplate>
</asp:repeater>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:repeater>
</form>
</body>
</HTML>
<%@ 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 MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<!-- 绑定主表数据 -->
<asp:repeater id="myRepeater" runat="server">
<HeaderTemplate>
<table border="0" bgcolor="lightblue">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="background-color: lightgray">
<b>
<%# DataBinder.Eval(Container.DataItem,"TypeID") %>
<%# DataBinder.Eval(Container.DataItem,"TypeName") %>
</b>
<br>
<!-- 绑定子表数据 -->
<asp:repeater id="childRepeater" datasource='<%# ((DataRowView)Container.DataItem)
.Row.GetChildRows("TypeRelation") %>' runat="server">
<itemtemplate>
<table border="0" bgcolor="#ffcc33">
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem,"[\"TypeID\"]") %>
<%# DataBinder.Eval(Container.DataItem, "[\"TypeDetail\"]")%>
<br>
</td>
</tr>
</table>
</itemtemplate>
</asp:repeater>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp: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;
namespace 数据绑定控件同时显示主表和子表数据
{
/// <summary>
/// 数据绑定控件同时显示主表和子表数据
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater myRepeater;
private void Page_Load(object sender, System.EventArgs e)
{
DataSet ds = new DataSet();
//1.构造主表
DataTable dtTypeParent=new DataTable();
dtTypeParent.Columns.Add(new DataColumn("TypeID",typeof(int)));
dtTypeParent.Columns.Add(new DataColumn("TypeName",typeof(string)));
//给主表添加两条记录
DataRow drParent1 = dtTypeParent.NewRow();
drParent1["TypeID"] = 1;
drParent1["TypeName"] = "水果";
dtTypeParent.Rows.Add(drParent1);
DataRow drParent2 = dtTypeParent.NewRow();
drParent2["TypeID"] = 2;
drParent2["TypeName"] = "玩具";
dtTypeParent.Rows.Add(drParent2);
dtTypeParent.TableName="TypeParent";
dtTypeParent.PrimaryKey = new DataColumn[] {dtTypeParent.Columns["TypeID"]};
ds.Tables.Add(dtTypeParent);
//2.构造子表
DataTable dtTypeChild=new DataTable();
dtTypeChild.Columns.Add(new DataColumn("TypeID",typeof(int)));
dtTypeChild.Columns.Add(new DataColumn("TypeDetail",typeof(string)));
//给子表添加五条记录
DataRow drChild1 = dtTypeChild.NewRow();
drChild1["TypeID"] = 1;
drChild1["TypeDetail"] = "苹果";
dtTypeChild.Rows.Add(drChild1);
DataRow drChild2 = dtTypeChild.NewRow();
drChild2["TypeID"] = 1;
drChild2["TypeDetail"] = "桔子";
dtTypeChild.Rows.Add(drChild2);
DataRow drChild3 = dtTypeChild.NewRow();
drChild3["TypeID"] = 1;
drChild3["TypeDetail"] = "香蕉";
dtTypeChild.Rows.Add(drChild3);
DataRow drChild4 = dtTypeChild.NewRow();
drChild4["TypeID"] = 2;
drChild4["TypeDetail"] = "机器人";
dtTypeChild.Rows.Add(drChild4);
DataRow drChild5 = dtTypeChild.NewRow();
drChild5["TypeID"] = 2;
drChild5["TypeDetail"] = "小汽车";
dtTypeChild.Rows.Add(drChild5);
dtTypeChild.TableName="TypeChild";
ds.Tables.Add(dtTypeChild);
ds.Relations.Add("TypeRelation",
ds.Tables["TypeParent"].Columns["TypeID"],
ds.Tables["TypeChild"].Columns["TypeID"]);
myRepeater.DataSource = ds.Tables["TypeParent"];
Page.DataBind();
}
Web 窗体设计器生成的代码
}
}
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;
namespace 数据绑定控件同时显示主表和子表数据
{
/// <summary>
/// 数据绑定控件同时显示主表和子表数据
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater myRepeater;
private void Page_Load(object sender, System.EventArgs e)
{
DataSet ds = new DataSet();
//1.构造主表
DataTable dtTypeParent=new DataTable();
dtTypeParent.Columns.Add(new DataColumn("TypeID",typeof(int)));
dtTypeParent.Columns.Add(new DataColumn("TypeName",typeof(string)));
//给主表添加两条记录
DataRow drParent1 = dtTypeParent.NewRow();
drParent1["TypeID"] = 1;
drParent1["TypeName"] = "水果";
dtTypeParent.Rows.Add(drParent1);
DataRow drParent2 = dtTypeParent.NewRow();
drParent2["TypeID"] = 2;
drParent2["TypeName"] = "玩具";
dtTypeParent.Rows.Add(drParent2);
dtTypeParent.TableName="TypeParent";
dtTypeParent.PrimaryKey = new DataColumn[] {dtTypeParent.Columns["TypeID"]};
ds.Tables.Add(dtTypeParent);
//2.构造子表
DataTable dtTypeChild=new DataTable();
dtTypeChild.Columns.Add(new DataColumn("TypeID",typeof(int)));
dtTypeChild.Columns.Add(new DataColumn("TypeDetail",typeof(string)));
//给子表添加五条记录
DataRow drChild1 = dtTypeChild.NewRow();
drChild1["TypeID"] = 1;
drChild1["TypeDetail"] = "苹果";
dtTypeChild.Rows.Add(drChild1);
DataRow drChild2 = dtTypeChild.NewRow();
drChild2["TypeID"] = 1;
drChild2["TypeDetail"] = "桔子";
dtTypeChild.Rows.Add(drChild2);
DataRow drChild3 = dtTypeChild.NewRow();
drChild3["TypeID"] = 1;
drChild3["TypeDetail"] = "香蕉";
dtTypeChild.Rows.Add(drChild3);
DataRow drChild4 = dtTypeChild.NewRow();
drChild4["TypeID"] = 2;
drChild4["TypeDetail"] = "机器人";
dtTypeChild.Rows.Add(drChild4);
DataRow drChild5 = dtTypeChild.NewRow();
drChild5["TypeID"] = 2;
drChild5["TypeDetail"] = "小汽车";
dtTypeChild.Rows.Add(drChild5);
dtTypeChild.TableName="TypeChild";
ds.Tables.Add(dtTypeChild);
ds.Relations.Add("TypeRelation",
ds.Tables["TypeParent"].Columns["TypeID"],
ds.Tables["TypeChild"].Columns["TypeID"]);
myRepeater.DataSource = ds.Tables["TypeParent"];
Page.DataBind();
}
Web 窗体设计器生成的代码
}
}