aspx
HTML code
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
DataListNesting.aspx.cs
"
Inherits
=
"
DataListNesting
"
%>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 无标题页 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
< asp:DataList ID ="DataList1" runat ="server" >
< ItemTemplate >
< asp:Label ID ="Label1" runat ="server" Text ='<%# Eval("OrderID") % > '> </ asp:Label >
< asp:Label ID ="Label2" runat ="server" Text ='<%# Eval("CustomerID") % > '> </ asp:Label >
< asp:DataList ID ="DataList2" runat ="server" DataSource ='<%# (Container.DataItem as System.Data.DataRowView).Row.GetChildRows("myRelation") % > '>
< ItemTemplate >
< asp:Label ID ="Label1" runat ="server" Text ='<%# (Container.DataItem as System.Data.DataRow)["ProductID"] % > '> </ asp:Label >
< asp:Label ID ="Label2" runat ="server" Text ='<%# (Container.DataItem as System.Data.DataRow)["UnitPrice"] % > '> </ asp:Label >
< asp:Label ID ="Label3" runat ="server" Text ='<%# (Container.DataItem as System.Data.DataRow)["Quantity"] % > '> </ asp:Label >
</ ItemTemplate >
</ asp:DataList >
</ ItemTemplate >
</ asp:DataList >
</ div >
</ form >
</ body >
</ html >
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 无标题页 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
< asp:DataList ID ="DataList1" runat ="server" >
< ItemTemplate >
< asp:Label ID ="Label1" runat ="server" Text ='<%# Eval("OrderID") % > '> </ asp:Label >
< asp:Label ID ="Label2" runat ="server" Text ='<%# Eval("CustomerID") % > '> </ asp:Label >
< asp:DataList ID ="DataList2" runat ="server" DataSource ='<%# (Container.DataItem as System.Data.DataRowView).Row.GetChildRows("myRelation") % > '>
< ItemTemplate >
< asp:Label ID ="Label1" runat ="server" Text ='<%# (Container.DataItem as System.Data.DataRow)["ProductID"] % > '> </ asp:Label >
< asp:Label ID ="Label2" runat ="server" Text ='<%# (Container.DataItem as System.Data.DataRow)["UnitPrice"] % > '> </ asp:Label >
< asp:Label ID ="Label3" runat ="server" Text ='<%# (Container.DataItem as System.Data.DataRow)["Quantity"] % > '> </ asp:Label >
</ ItemTemplate >
</ asp:DataList >
</ ItemTemplate >
</ asp:DataList >
</ div >
</ form >
</ body >
</ html >
aspx.cs
-
C# code
-
using System.Data.SqlClient; public partial class DataListNesting : System.Web.UI.Page { private void BindList() { SqlConnection cn = new SqlConnection( @" server=.\sqlexpress;uid=sa;pwd=;database=northwind; " ); SqlDataAdapter da = new SqlDataAdapter( " select OrderID, CustomerID from Orders; select OrderID, ProductID, UnitPrice, Quantity from [Order Details] " , cn); DataSet ds = new DataSet(); cn.Open(); da.Fill(ds); cn.Close(); ds.Tables[ 0 ].TableName = " Orders " ; ds.Tables[ 1 ].TableName = " OrderDetails " ; DataRelation ordersToOrderDetails = ds.Relations.Add( " myRelation " , ds.Tables[ " Orders " ].Columns[ " OrderID " ], ds.Tables[ " OrderDetails " ].Columns[ " OrderID " ]); DataList1.DataSource = ds.Tables[ " Orders " ]; DataList1.DataBind(); } private void Page_Load( object sender, System.EventArgs e) { if ( ! IsPostBack) { BindList(); } }