list列表下嵌套多个list_datalist和select的区别

list列表下嵌套多个list_datalist和select的区别 aspxviewplaincopytoclipboardprint?%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”DataListNesting.aspx.cs” Inherits=”DataListNesting” %>  >  html xmlns=”http://www.w3.org/1

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用
 

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”>  
  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”>  
  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.     private void Page_Load(object sender, System.EventArgs e)   
  29.     {   
  30.         if (!IsPostBack)   
  31.         {   
  32.             BindList();   
  33.         }   
  34.     }   
  35.   
  36.     protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)   
  37.     {   
  38.         DataList datalist2;   
  39.         if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)   
  40.         {   
  41.             datalist2 = e.Item.FindControl(“DataList2”) as DataList;   
  42.             if (datalist2 != null)   
  43.             {   
  44.                 SqlConnection cn = new SqlConnection(@“server=./sqlexpress;uid=sa;pwd=;database=northwind;”);   
  45.                 SqlDataAdapter da = new SqlDataAdapter(“select ProductID, UnitPrice, Quantity from [Order Details] where orderID = @orderID”, cn);   
  46.                 da.SelectCommand.Parameters.AddWithValue(“@orderID”, (e.Item.DataItem as DataRowView)[“orderID”]);   
  47.                 DataSet ds = new DataSet();   
  48.                 cn.Open();   
  49.                 da.Fill(ds);   
  50.                 cn.Close();   
  51.                 datalist2.DataSource = ds.Tables[0].DefaultView;   
  52.                 datalist2.DataBind();   
  53.             }   
  54.         }   
  55.     }   
  56. }  

使用DataRelation的方式仍然可以使用ItemDataBound事件,有兴趣的朋友可以参考上面的注释代码 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/182417.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • C# 串口通信 stm32 电机

    C# 串口通信 stm32 电机前几天已经完成了stm32通过PWM对电机的控制,这几天趁上班之余,也完成了c#通过串口通信控制电机的运行。界面如下好久没写文章了,发现非常不擅长分享和表达,第一反应是演示出来,可惜这里不能有动画,功能不强大啊。哪天有空了,把上位机代码和下位机代码上传上来。代码已经上传,请查看我的资源页。…

    2022年6月3日
  • 【mysql 索引】mysql 添加索引

    【mysql 索引】mysql 添加索引主要内容:1、什么是索引2、添加索引sql3、索引所用的算法4、补充下概念5、索引的缺点6、经典案例一、什么是索引?  索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存。如果没有索引,执行查询时MySQL必须从第一个记录开始扫描整个表的所有记录,直至找到符合要求的记录。表里面的记录数量越多,这个操作的代价就越高。如果作为搜索条件的列上已经创建

    2022年6月2日
  • mybatis-plus 在线激活码_在线激活

    (mybatis-plus 在线激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月26日
  • 计算机网络实验四访问控制列表NAT应用

    计算机网络实验四访问控制列表NAT应用计算机网络实验四访问控制列表NAT应用

    2022年7月23日
  • goland 激活【2021免费激活】

    (goland 激活)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html1M…

    2022年3月28日
  • Python2安装_python安装不了

    Python2安装_python安装不了py2exe相信大家都知道,一个能够打包Python程序变成可执行文件的Python库。用起来很简单,但是好像还是有限,像opencv这样的库还是没有办法打包等。我安装的时候遇到了一小点问题,分享给大家:Python3安装py2exe安装直接使用pipinstallpy2exePython2安装py2exepython2安装的时候无法直接使用pipinstallpy2exe,…

    2022年9月10日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号