DataList嵌套DataList(页面绑定后台代码使用ItemDataBound事件实现 纯代码)

  1.  
    项模板直接拉一个RadioButton,然后写JS代码,如下:

     <script language="javascript">
            function check(radio) {
                var radioButtons = document.getElementsByTagName("input");
                for (var i = 0; i < radioButtons.length; i++) {
                    if (radioButtons[i].type == "radio") {
                        if (radioButtons[i] == radio) {
                            continue;
                        }
                        else {
                            radioButtons[i].checked = false;
                        }
                    
                    }
                }
            }
        </script>

    源文件:
            <asp:DataList ID="DataList1" runat="server">
                <ItemTemplate>              
                        <asp:RadioButton ID="RadioButton1" onclick="check(this)" runat="server" />
                </ItemTemplate>
            </asp:DataList>

 

 

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListNesting.aspx.cs" Inherits="DataListNesting" %>
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head runat="server">
  4.   <title>DataListNesting</title>
  5. </head>
  6. <body>
  7. <form id="form1" runat="server">
  8. <div>
  9. <asp:DataList ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
  10.   <ItemTemplate>
  11.       <asp:Label ID="Label1" runat="server" Text='<%# Eval("OrderID") %>'></asp:Label>
  12.       <asp:Label ID="Label2" runat="server" Text='<%# Eval("CustomerID") %>'></asp:Label>
  13.       <asp:DataList ID="DataList2" runat="server">
  14.           <ItemTemplate>
  15.               <asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>
  16.               <asp:Label ID="Label2" runat="server" Text='<%# Eval("UnitPrice") %>'></asp:Label>
  17.               <asp:Label ID="Label3" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
  18.           </ItemTemplate>
  19.       </asp:DataList>
  20.   </ItemTemplate>
  21. </asp:DataList>
  22. </div>
  23. </form>
  24. </body>
  25. </html>
复制代码
  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. public partial class DataListNesting : System.Web.UI.Page
  13. {
  14.   private void BindList()
  15.   {
  16.       SqlConnection cn = new SqlConnection(@"server=.\sqlexpress;uid=sa;pwd=;database=northwind");
  17.       SqlDataAdapter da = new SqlDataAdapter("select OrderID, CustomerID from Orders", cn);
  18.       DataSet ds = new DataSet();
  19.       cn.Open();
  20.       da.Fill(ds);
  21.       cn.Close();
  22.       DataList1.DataSource = ds.Tables[0].DefaultView;
  23.       DataList1.DataKeyField = "orderID";
  24.       DataList1.DataBind();
  25.   }

  26.   private void Page_Load(object sender, System.EventArgs e)
  27.   {
  28.       if (!IsPostBack)
  29.       {
  30.           BindList();
  31.       }
  32.   }

  33.   protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
  34.   {
  35.       DataList datalist2;
  36.       if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  37.       {
  38.           datalist2 = e.Item.FindControl("DataList2") as DataList;
  39.           if (datalist2 != null)
  40.           {
  41.               SqlConnection cn = new SqlConnection(@"server=.\sqlexpress;uid=sa;pwd=;database=northwind;");
  42.               SqlDataAdapter da = new SqlDataAdapter("select ProductID, UnitPrice, Quantity from [Order Details] where orderID = @orderID", cn);
  43.               da.SelectCommand.Parameters.AddWithValue("@orderID", (e.Item.DataItem as DataRowView)["orderID"]);
  44.               DataSet ds = new DataSet();
  45.               cn.Open();
  46.               da.Fill(ds);
  47.               cn.Close();
  48.               datalist2.DataSource = ds.Tables[0].DefaultView;
  49.               datalist2.DataBind();
  50.           }
  51.       }
  52.   }
  53. }
原文地址:https://www.cnblogs.com/liufei88866/p/1908081.html