DataList嵌套DataList(使用DataRelation实现 纯代码)

来源:互联网 发布:网站美工都做什么 编辑:程序博客网 时间:2024/05/22 15:20
 aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListNesting.aspx.cs" Inherits="DataListNesting" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml" >  
  4. <head runat="server">  
  5.     <title>DataListNesting</title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.     <asp:DataList ID="DataList1" runat="server">  
  11.         <ItemTemplate>  
  12.             <asp:Label ID="Label1" runat="server" Text='<%# Eval("OrderID") %>'></asp:Label>  
  13.             <asp:Label ID="Label2" runat="server" Text='<%# Eval("CustomerID") %>'></asp:Label>  
  14.             <asp:DataList ID="DataList2" runat="server" DataSource='<%# (Container.DataItem as System.Data.DataRowView).Row.GetChildRows("myRelation") %>'>  
  15.                 <ItemTemplate>  
  16.                     <asp:Label ID="Label1" runat="server" Text='<%# (Container.DataItem as System.Data.DataRow)["ProductID"] %>'></asp:Label>  
  17.                     <asp:Label ID="Label2" runat="server" Text='<%# (Container.DataItem as System.Data.DataRow)["UnitPrice"] %>'></asp:Label>  
  18.                     <asp:Label ID="Label3" runat="server" Text='<%# (Container.DataItem as System.Data.DataRow)["Quantity"] %>'></asp:Label>  
  19.                 </ItemTemplate>  
  20.             </asp:DataList>  
  21.         </ItemTemplate>  
  22.     </asp:DataList>  
  23.     </div>  
  24.     </form>  
  25. </body>  
  26. </html>  
aspx.cs
  1. using System;   
  2. using System.Data;   
  3. using System.Configuration;   
  4. using System.Collections;   
  5. using System.Web;   
  6. using System.Web.Security;   
  7. using System.Web.UI;   
  8. using System.Web.UI.WebControls;   
  9. using System.Web.UI.WebControls.WebParts;   
  10. using System.Web.UI.HtmlControls;   
  11. using System.Data.SqlClient;   
  12.   
  13. public partial class DataListNesting : System.Web.UI.Page   
  14. {   
  15.     private void BindList()   
  16.     {   
  17.         SqlConnection cn = new SqlConnection(@"server=./sqlexpress;uid=sa;pwd=;database=northwind;");   
  18.         SqlDataAdapter da = new SqlDataAdapter("select OrderID, CustomerID from Orders; select OrderID, ProductID, UnitPrice, Quantity from [Order Details]", cn);   
  19.         DataSet ds = new DataSet();   
  20.         cn.Open();   
  21.         da.Fill(ds);   
  22.         cn.Close();   
  23.         ds.Tables[0].TableName = "Orders";   
  24.         ds.Tables[1].TableName = "OrderDetails";   
  25.   
  26.         DataRelation ordersToOrderDetails = ds.Relations.Add("myRelation", ds.Tables["Orders"].Columns["OrderID"], ds.Tables["OrderDetails"].Columns["OrderID"]);   
  27.   
  28.         DataList1.DataSource = ds.Tables["Orders"];   
  29.         DataList1.DataBind();   
  30.     }   
  31.   
  32.     private void Page_Load(object sender, System.EventArgs e)   
  33.     {   
  34.         if (!IsPostBack)   
  35.         {   
  36.             BindList();   
  37.         }   
  38.     }   
  39.   
  40.     //protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)   
  41.     //{   
  42.     //    DataList DataList2;   
  43.     //    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)   
  44.     //    {   
  45.     //        DataList2 = e.Item.FindControl("DataList2") as DataList;   
  46.     //        DataList2.DataSource = ((DataRowView)(e.Item.DataItem)).Row.GetChildRows("myRelation");   
  47.     //        DataList2.DataBind();   
  48.     //    }   
  49.     //}   
  50. }  
原创粉丝点击