Controls | data | display
(i). Display Effect Chart
(ii) Code
1. Front Interface code:
<%@ Page language= "C #" codebehind= "WebForm1.aspx.cs" autoeventwireup= "false" inherits= "data-bound controls display both primary and child table data. 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" >
<!--binding Master table data-->
<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>
<!--binding child table Data-->
<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. Descendant code
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 data-bound controls display both primary and child table data
{
<summary>
Data-bound controls display both primary and child table data
</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. Construct the main table
DataTable dttypeparent=new DataTable ();
DTTYPEPARENT.COLUMNS.ADD (New DataColumn ("typeID", typeof (int));
DTTYPEPARENT.COLUMNS.ADD (New DataColumn ("TypeName", typeof (String));
Add two records to the main table
DataRow drParent1 = Dttypeparent.newrow ();
drparent1["typeID"] = 1;
drparent1["TypeName"] = "fruit";
DTTYPEPARENT.ROWS.ADD (DRPARENT1);
DataRow DrParent2 = Dttypeparent.newrow ();
drparent2["typeID"] = 2;
drparent2["TypeName"] = "toy";
DTTYPEPARENT.ROWS.ADD (DrParent2);
Dttypeparent.tablename= "Typeparent";
Dttypeparent.primarykey = new datacolumn[] {dttypeparent.columns["typeid"]};
Ds. Tables.add (dttypeparent);
2. Constructing Child tables
DataTable dttypechild=new DataTable ();
DTTYPECHILD.COLUMNS.ADD (New DataColumn ("typeID", typeof (int));
DTTYPECHILD.COLUMNS.ADD (New DataColumn ("Typedetail", typeof (String));
Add five records to a child table
DataRow drChild1 = Dttypechild.newrow ();
drchild1["typeID"] = 1;
drchild1["Typedetail"] = "Apple";
DTTYPECHILD.ROWS.ADD (DRCHILD1);
DataRow drChild2 = Dttypechild.newrow ();
drchild2["typeID"] = 1;
drchild2["Typedetail"] = "orange";
DTTYPECHILD.ROWS.ADD (DRCHILD2);
DataRow drChild3 = Dttypechild.newrow ();
drchild3["typeID"] = 1;
drchild3["Typedetail"] = "banana";
DTTYPECHILD.ROWS.ADD (DRCHILD3);
DataRow drChild4 = Dttypechild.newrow ();
drchild4["typeID"] = 2;
drchild4["Typedetail"] = "robot";
DTTYPECHILD.ROWS.ADD (DRCHILD4);
DataRow drChild5 = Dttypechild.newrow ();
drchild5["typeID"] = 2;
drchild5["Typedetail"] = "small Car";
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 ();
}
Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
}
}