DataList嵌套DataList(使用SqlDataSource实现)

  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" DataSourceID="SqlDataSource1">  
  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" DataSourceID="SqlDataSource2">  
  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.         <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">  
  22.             <SelectParameters>  
  23.                 <asp:Parameter Name="orderID" />  
  24.             </SelectParameters>  
  25.         </asp:SqlDataSource>  
  26.     </ItemTemplate>  
  27.     </asp:DataList>  
  28.         <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>  
  29. </div>  
  30. </form>  
  31. </body>  
  32. </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.   
  12. public partial class DataListNesting : System.Web.UI.Page   
  13. {   
  14.     private void Page_Load(object sender, System.EventArgs e)   
  15.     {   
  16.   
  17.     }   
  18.   
  19.     protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)   
  20.     {   
  21.         SqlDataSource sqlDataSource2;   
  22.         if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)   
  23.         {   
  24.             sqlDataSource2 = e.Item.FindControl("SqlDataSource2"as SqlDataSource;   
  25.             if (sqlDataSource2 != null)   
  26.             {   
  27.                 sqlDataSource2.SelectParameters["orderID"].DefaultValue = (e.Item.DataItem as DataRowView)["orderID"].ToString();   
  28.             }   
  29.         }   
  30.     }   
  31. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现DataList里面嵌套DataList的折叠菜单,可以按照以下步骤进行: 1. 在外层DataList的ItemTemplate中嵌套一个内层DataList,并将其Visible属性设置为false。 2. 在外层DataList的ItemDataBound事件中获取内层DataList的引用,并绑定内层DataList的数据源,最后将内层DataList的Visible属性设置为true。 3. 在外层DataList的ItemCommand事件中处理内层DataList的展开和折叠,可以通过判断CommandName属性来确定是展开还是折叠,然后设置内层DataList的Visible属性即可。 下面是一个示例代码片段: ``` <asp:DataList ID="outerDataList" runat="server" OnItemDataBound="outerDataList_ItemDataBound" OnItemCommand="outerDataList_ItemCommand"> <ItemTemplate> <div> <a href="#" onclick="return false;" commandname="expand" class="expand">展开</a> <a href="#" onclick="return false;" commandname="collapse" class="collapse" style="display:none;">折叠</a> <asp:DataList ID="innerDataList" runat="server" Visible="false"> <ItemTemplate> <div><%#Eval("Name")%></div> </ItemTemplate> </asp:DataList> </div> </ItemTemplate> </asp:DataList> protected void outerDataList_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DataList innerDataList = e.Item.FindControl("innerDataList") as DataList; innerDataList.DataSource = GetInnerDataListDataSource(); innerDataList.DataBind(); innerDataList.Visible = true; } } protected void outerDataList_ItemCommand(object source, DataListCommandEventArgs e) { if (e.CommandName == "expand") { DataList innerDataList = e.Item.FindControl("innerDataList") as DataList; innerDataList.Visible = true; e.Item.FindControl("expand").Visible = false; e.Item.FindControl("collapse").Visible = true; } else if (e.CommandName == "collapse") { DataList innerDataList = e.Item.FindControl("innerDataList") as DataList; innerDataList.Visible = false; e.Item.FindControl("expand").Visible = true; e.Item.FindControl("collapse").Visible = false; } } ``` 在这个示例代码中,我们在外层DataList的ItemTemplate中嵌套了一个内层DataList,并且将其Visible属性设置为false。在外层DataList的ItemDataBound事件中获取内层DataList的引用,并绑定内层DataList的数据源,最后将内层DataList的Visible属性设置为true。在外层DataList的ItemCommand事件中处理内层DataList的展开和折叠,通过判断CommandName属性来确定是展开还是折叠,然后设置内层DataList的Visible属性即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值