DataList嵌套DataList 子DataList访问父DataList数据(1. 页面绑定后台代码实现 纯代码)

本文展示了如何在ASP.NET中实现DataList嵌套DataList的数据绑定。通过后台代码,子DataList(DataList2)能够访问父DataList(DataList1)的数据,具体体现在子DataList中Label1显示了父DataList的`CustomerID`值。
摘要由CSDN通过智能技术生成

aspx页面代码

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

<%@ 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 id="Head1" runat="server"> 
    <title>DataListNesting</title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
<asp:DataList ID="DataList1" runat="server"> 
    <ItemTemplate> 
        <div>
            <asp:Label ID="Label1" runat="server" Text='<%# Eval("OrderID") %>'></asp:Label> 
            <asp:Label ID="Label2" runat="server" Text='<%# Eval("CustomerID") %>'></asp:Label>         
        </div>
        <asp:DataList ID="DataList2" runat="server" DataSource='<%# GetDetails(Eval("OrderID").ToString()) %>'> 
            <ItemTemplate> 
                <div>
                <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval( (Container.NamingContainer.NamingContainer as DataListItem).DataItem, "CustomerID" ) %>'></asp:Label> 
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label> 
                <asp:Label ID="Label3" runat="server" Text='<%# Eval("UnitPrice") %>'></asp:Label> 
                <asp:Label ID="Label4" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>                
                </div>
            </ItemTemplate> 
        </asp:DataList> 
    </ItemTemplate> 
</asp:DataList> 
</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;
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", cn);
        DataSet ds = new DataSet();
        cn.Open();
        da.Fill(ds);
        cn.Close();
        DataList1.DataSource = ds.Tables[0].DefaultView;
        DataList1.DataKeyField = "orderID";
        DataList1.DataBind();
    }

    protected DataTable GetDetails(string orderID)
    {
        SqlConnection cn = new SqlConnection(@"server=./sqlexpress;uid=sa;pwd=;database=northwind;");
        SqlDataAdapter da = new SqlDataAdapter("select ProductID, UnitPrice, Quantity from [Order Details] where orderID = @orderID", cn);
        da.SelectCommand.Parameters.AddWithValue("@orderID", orderID);
        DataSet ds = new DataSet();
        cn.Open();
        da.Fill(ds);
        cn.Close();
        return ds.Tables[0];
    }

    private void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {
            BindList();
        }
    }
}

其中DataList2中的Label1得到的是父容器数据项中的值

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值