方法一: 前台: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListNesting.aspx.cs" Inherits="kzpic_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("midclass_id") %>'></asp:Label> <asp:Label ID="Label2" runat="server" Text='<%# Eval("midclass_name") %>'></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)["midclass_id"] %>'></asp:Label> <asp:Label ID="Label2" runat="server" Text='<%# (Container.DataItem as System.Data.DataRow)["title"] %>'></asp:Label> <asp:Label ID="Label3" runat="server" Text='<%# (Container.DataItem as System.Data.DataRow)["small_pic"] %>'></asp:Label> </ItemTemplate> </asp:DataList> </ItemTemplate> </asp:DataList> </div> </form> </body> </html> 后台: 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; using System.Data.SqlClient;
public partial class kzpic_DataListNesting : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindList(); } } private void BindList() { //从配置文件获取数据库连接串 string strConnection = ConfigurationManager.AppSettings["conStr"]; SqlConnection cn = new SqlConnection(strConnection); cn.Open(); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter("select * from midclassname where bigclass_id=7; select top 12 * from kztp", cn); 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["midclass_id"], ds.Tables["OrderDetails"].Columns["midclass_id"]); DataList1.DataSource = ds.Tables["Orders"]; this.DataList1.DataBind();//执行绑定 } }
方法二: 前台: <%@ 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>DataListNesting</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> 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/amandag/archive/2008/06/11/2537364.aspx 后台: 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; 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(); } } } 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/amandag/archive/2008/06/11/2537364.aspx
|