aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListNesting.aspx.cs" Inherits="DataListNesting" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html XMLns=">
<head runat="server">
<title>DataListNesting</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound" DataSourceID="SqlDataSource1">
<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" DataSourceID="SqlDataSource2">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("UnitPrice") %>'></asp:Label>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="Data Source=.sqlexpress;Initial Catalog=Northwind;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="select ProductID, UnitPrice, Quantity from [Order Details] where orderID = @orderID">
<SelectParameters>
<asp:Parameter Name="orderID" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.sqlexpress;Initial Catalog=Northwind;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="select OrderID, CustomerID from Orders"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class DataListNesting : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
SqlDataSource SqlDataSource2;
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
SqlDataSource2 = e.Item.FindControl("SqlDataSource2") as SqlDataSource;
if (SqlDataSource2 != null)
{
SqlDataSource2.SelectParameters["orderID"].DefaultValue = (e.Item.DataItem as DataRowView)["orderID"].ToString();
}
}
}
}