如何使用嵌套转发器在 asp.net 网络表单中显示带复选框的母公司和子公司列表 select

how to show list of parent company and sub companies with checkbox to select in asp.net webform using nested repeaters

Company table in SQL server using asp.net web forms to show all of companies first with a parent company and sub-companies under the parent company, with a checkbox列出 select 个公司。在第一个 SQL SP 中,我从数据 table 中获取母公司列表,而该公司 ID 来自第一个数据 table( ex:1,2,3) 循环到在第二个数据表中获取子公司列表

我已经尝试使用嵌套中继器来处理 2 个数据 tables 但不确定如何实现这个以及哪个是最好的控制

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            DataTable dtx = Getparentfirm();
            DataTable dso = GetSuboffice(dtx);
            R1.DataSource = dtx;
            R1.DataBind();
            R2.DataSource = dso;
            R2.DataBind();
        }

    }

    private DataTable Getparentfirm()
    {
        using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=test;Integrated Security=True"))
        {
            SqlDataAdapter sda = new SqlDataAdapter("select * from company where parentcompany is NULL", conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            return dt;
        }

    }
    private DataTable GetSuboffice(DataTable dt)
    {
        DataTable dtz = new DataTable();
        using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=test;Integrated Security=True"))
            foreach (DataRow row in dt.Rows)
            {                           
            SqlDataAdapter sda = new SqlDataAdapter("select  id, fname , lname from customer where cid =" + row["cid"].ToString(), conn);                           
            sda.Fill(dtz);
            return dtz;
        }
        return null;
    }

       <asp:Repeater ID="R1" runat="server">
            <ItemTemplate>
                    name:<%# Eval("companyname") %></td>
            </ItemTemplate>
            <asp:Repeater ID="R2" runat="server">
                    <ItemTemplate>
                        ID :<%# Eval("companyid") %>
                        Name:<%# Eval("companyname") %>

                </ItemTemplate>
            </asp:Repeater>
        </asp:Repeater>

如何将 2 个数据table绑定到嵌套的转发器以获得预期的输出。任何帮助都会很棒

预期输出

      parentcompanyid 1     parent company one          checkbox
        sub office 1.1        sub office one            checkbox
        sub office 1.2        sub office two            checkbox
      parent comapnyid 2    parent company two          checkbox 
         sub office 2.1        sub office one           checkbox
        sub office 2.2        sub office two            checkbox

当您使用 Repeater 时,请确保您的所有内容都在模板节点内,例如 <ItemTemplate> - 即使是嵌套的 Repeater。

一旦标记正确,您可以将一个 OnItemDataBound 事件附加到父 Repeater,然后使用该项目的 ID 获取数据以绑定嵌套的 Repeater,然后绑定它。例如:

ASPX

        <asp:Repeater ID="R1" runat="server" OnItemDataBound="R1_ItemDataBound">
            <ItemTemplate>
                  <asp:HiddenField runat="server" ID="CompanyId" value='<%# Eval("id") %>' />
                  <div>name:<%# Eval("companyname") %></div>

                  <asp:Repeater ID="R2" runat="server">
                    <ItemTemplate>
                        ID :<%# Eval("companyid") %><br>
                        Name:<%# Eval("companyname") %>
                    </ItemTemplate>
                  </asp:Repeater>
            </ItemTemplate>                
        </asp:Repeater>

CS

    protected void Page_Load(object sender, EventArgs e)
    {   
        if (!Page.IsPostBack)
        {
            DataTable dtx = Getparentfirm();
            R1.DataSource = dtx;
            R1.DataBind();
        }  
    }    

    protected void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
          // Get companyId from hidden field 
          // or you could get it from e.Item.DataItem which should have the data for this row of data
          var CompanyID = (HiddenField)e.Item.FindControl("CompanyId");
          var id = Convert.ToInt32(CompanyID.Value);

          var R2 = (Repeater)e.Item.FindControl("R2");
          var dso = GetData(id); // Get sub companies based on this company id

          R2.DataSource = dso;
          R2.DataBind();
       }  

类似的东西