当前位置导航:炫浪网>>网络学院>>网页制作>>XML/WebService教程

用SqlDataSource实现DataList嵌套DataList

aspx

 <%@ 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" OnItemDataBound="DataList1_ItemDataBound" DataSourceID="SqlDataSource1"> 
        <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" DataSourceID="SqlDataSource2"> 
            <ItemTemplate> 
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label> 
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("UnitPrice") %>'></asp:Label> 
                <asp:Label ID="Label3" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label> 
            </ItemTemplate> 
        </asp:DataList> 
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="select ProductID, UnitPrice, Quantity from [Order Details] where orderID = @orderID"> 
            <SelectParameters> 
                <asp:Parameter Name="orderID" /> 
            </SelectParameters> 
        </asp:SqlDataSource> 
    </ItemTemplate> 
    </asp:DataList> 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="select OrderID, CustomerID from Orders"></asp:SqlDataSource> 
</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;  
 
public partial class DataListNesting : System.Web.UI.Page  
{  
    private void Page_Load(object sender, System.EventArgs e)  
    {  
 
    }  
 
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)  
    {  
        SqlDataSource sqlDataSource2;  
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)  
        {  
            sqlDataSource2 = e.Item.FindControl("SqlDataSource2") as SqlDataSource;  
            if (sqlDataSource2 != null)  
            {  
                sqlDataSource2.SelectParameters["orderID"].DefaultValue = (e.Item.DataItem as DataRowView)["orderID"].ToString();  
            }  
        }  
    }  

相关内容
赞助商链接