(一).显示效果图
(二)代码
1.前台界面代码:
<%@ 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 >
<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>
2. 后代代码
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();
}
#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
}
}
新闻热点
疑难解答