大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
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”>
- <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”>
- <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>
- </ItemTemplate>
- </asp:DataList>
- </div>
- </form>
- </body>
- </html>
<%@ 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"> <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"> <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> </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();
- }
- private void Page_Load(object sender, System.EventArgs e)
- {
- if (!IsPostBack)
- {
- BindList();
- }
- }
- protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
- {
- DataList datalist2;
- if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
- {
- datalist2 = e.Item.FindControl(“DataList2”) as DataList;
- if (datalist2 != null)
- {
- 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”, (e.Item.DataItem as DataRowView)[“orderID”]);
- DataSet ds = new DataSet();
- cn.Open();
- da.Fill(ds);
- cn.Close();
- datalist2.DataSource = ds.Tables[0].DefaultView;
- datalist2.DataBind();
- }
- }
- }
- }
使用DataRelation的方式仍然可以使用ItemDataBound事件,有兴趣的朋友可以参考上面的注释代码
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/182417.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...