DataList嵌套DataList(页面绑定后台代码使用ItemDataBound事件实现 纯代码)

来源:互联网 发布:网站美工都做什么 编辑:程序博客网 时间:2024/05/18 09:23
 

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" OnItemDataBound="DataList1_ItemDataBound">  
  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">  
  15.             <ItemTemplate>  
  16.                 <asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>  
  17.                 <asp:Label ID="Label2" runat="server" Text='<%# Eval("UnitPrice") %>'></asp:Label>  
  18.                 <asp:Label ID="Label3" runat="server" Text='<%# Eval("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", cn);   
  19.         DataSet ds = new DataSet();   
  20.         cn.Open();   
  21.         da.Fill(ds);   
  22.         cn.Close();   
  23.         DataList1.DataSource = ds.Tables[0].DefaultView;   
  24.         DataList1.DataKeyField = "orderID";   
  25.         DataList1.DataBind();   
  26.     }   
  27.   
  28.     private void Page_Load(object sender, System.EventArgs e)   
  29.     {   
  30.         if (!IsPostBack)   
  31.         {   
  32.             BindList();   
  33.         }   
  34.     }   
  35.   
  36.     protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)   
  37.     {   
  38.         DataList datalist2;   
  39.         if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)   
  40.         {   
  41.             datalist2 = e.Item.FindControl("DataList2"as DataList;   
  42.             if (datalist2 != null)   
  43.             {   
  44.                 SqlConnection cn = new SqlConnection(@"server=./sqlexpress;uid=sa;pwd=;database=northwind;");   
  45.                 SqlDataAdapter da = new SqlDataAdapter("select ProductID, UnitPrice, Quantity from [Order Details] where orderID = @orderID", cn);   
  46.                 da.SelectCommand.Parameters.AddWithValue("@orderID", (e.Item.DataItem as DataRowView)["orderID"]);   
  47.                 DataSet ds = new DataSet();   
  48.                 cn.Open();   
  49.                 da.Fill(ds);   
  50.                 cn.Close();   
  51.                 datalist2.DataSource = ds.Tables[0].DefaultView;   
  52.                 datalist2.DataBind();   
  53.             }   
  54.         }   
  55.     }   
  56. }  

使用DataRelation的方式仍然可以使用ItemDataBound事件,有兴趣的朋友可以参考上面的注释代码 

原创粉丝点击