DataList嵌套DataList(页面绑定后台代码实现 纯代码)

 

aspx页面代码

内嵌的DataList在页面使用后台的GetDetails方法绑定

  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='<%# GetDetails(Eval("OrderID").ToString()) %>'>  
  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.     protected DataTable GetDetails(string orderID)   
  29.     {   
  30.         SqlConnection cn = new SqlConnection(@"server=./sqlexpress;uid=sa;pwd=;database=northwind;");   
  31.         SqlDataAdapter da = new SqlDataAdapter("select ProductID, UnitPrice, Quantity from [Order Details] where orderID = @orderID", cn);   
  32.         da.SelectCommand.Parameters.AddWithValue("@orderID", orderID);   
  33.         DataSet ds = new DataSet();   
  34.         cn.Open();   
  35.         da.Fill(ds);   
  36.         cn.Close();   
  37.         return ds.Tables[0];   
  38.     }   
  39.   
  40.     private void Page_Load(object sender, System.EventArgs e)   
  41.     {   
  42.         if (!IsPostBack)   
  43.         {   
  44.             BindList();   
  45.         }   
  46.     }   
  47. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值