用SqlDataSource实现DataList嵌套DataList

ZDNet软件频道 时间:2008-07-10 作者:闵刚 | 中国IT实验室 我要评论()
本文关键词:嵌套 SqlDataSource XML 软件
用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" "
<html XMLns="> 
<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();  
            }  
        }  
    }  
}  

嵌套

SqlDataSource

XML

软件


百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134