locked
How can i Nest Datalist2 inside Nested Datalist1 RRS feed

  • Question

  • User-2074858223 posted

    I have a Nested Datalist A, but i want to Nest anotheR Datalist B inside the Nested Datalist A

    public partial class CS : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dlCustomers.DataSource = GetData("SELECT TOP 10 CustomerID,ContactName,City FROM Customers");
                dlCustomers.DataBind();
            }
        }
     
        private static DataTable GetData(string query)
        {
            string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = query;
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.Connection = con;
                        sda.SelectCommand = cmd;
                        using (DataSet ds = new DataSet())
                        {
                            DataTable dt = new DataTable();
                            sda.Fill(dt);
                            return dt;
                        }
                    }
                }
            }
        }
     
        protected void dlCustomers_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView dataRowView = e.Item.DataItem as DataRowView;
                string customerId = dataRowView["CustomerID"].ToString();
                DataList orderDataList = e.Item.FindControl("dlOrders") as DataList;
                orderDataList.DataSource = GetData("SELECT TOP 5 OrderId,OrderDate FROM Orders WHERE CustomerID = '" + customerId + "'");
                orderDataList.DataBind();
            }
        }
    }
    <div>
        <asp:DataList ID="dlCustomers" runat="server" OnItemDataBound="dlCustomers_ItemDataBound">
            <HeaderTemplate>
                <table>
                    <tr>
                        <th>
                            Name
                        </th>
                        <th>
                            City
                        </th>
                        <th>
                            Order
                        </th>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:Label ID="lblContactName" runat="server" Text='<%#Eval("ContactName")%>'></asp:Label>
                    </td>
                    <td>
                        <asp:Label ID="lblCity" runat="server" Text='<%#Eval("City")%>'></asp:Label>
                    </td>
                    <td>
                        <asp:DataList ID="dlOrders" runat="server">
                            <HeaderTemplate>
                                <table>
                                    <tr>
                                        <th>
                                            Order Id
                                        </th>
                                        <th>
                                            Order Date
                                        </th>
                                    </tr>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <tr>
                                    <td>
                                        <asp:Label ID="lblOrderId" Text='<%#Eval("OrderId")%>' runat="server" />
                                    </td>
                                    <td>
                                        <asp:Label ID="lblDate" Text='<%#Convert.ToDateTime(Eval("OrderDate")).ToString("dd/MM/yyyy")%>'
                                            runat="server" />
                                    </td>
                                </tr>
                            </ItemTemplate>
                            <FooterTemplate>
                                <asp:Label runat="server" Visible="<%#bool.Parse((dlCustomers.Items.Count==0).ToString())%>"
                                    Text="No Record Found!">
                                </asp:Label>
                                </table>
                            </FooterTemplate>
                        </asp:DataList>
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:DataList>
    </div>
    Saturday, December 2, 2017 12:18 PM

Answers

  • User-335504541 posted

    <g class="gr_ gr_11 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="11" data-gr-id="11">Hi</g> micah2012,

    Please try to use the following code:

    In <g class="gr_ gr_35 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="35" data-gr-id="35">aspx</g>:

                <div>
                    <asp:DataList ID="dlCustomers" runat="server" OnItemDataBound="dlCustomers_ItemDataBound">
                        <HeaderTemplate>
                            <table>
                                <tr>
                                    <th>Name
                                    </th>
                                    <th>City
                                    </th>
                                    <th>Order
                                    </th>
                                </tr>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <tr>
                                <td>
                                    <asp:Label ID="lblContactName" runat="server" Text='<%#Eval("ContactName")%>'></asp:Label>
                                </td>
                                <td>
                                    <asp:Label ID="lblCity" runat="server" Text='<%#Eval("City")%>'></asp:Label>
                                </td>
                                <td>
                                    <asp:DataList ID="dlOrders" runat="server" OnItemDataBound="dlOrders_ItemDataBound">
                                        <HeaderTemplate>
                                            <table>
                                                <tr>
                                                    <th>Order Id
                                                    </th>
                                                    <th>Order Date
                                                    </th>
                                                    <th>Order Detail
                                                    </th>
                                                </tr>
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <tr>
                                                <td>
                                                    <asp:Label ID="lblOrderId" Text='<%#Eval("OrderId")%>' runat="server" />
                                                </td>
                                                <td>
                                                    <asp:Label ID="lblDate" Text='<%#Convert.ToDateTime(Eval("OrderDate")).ToString("dd/MM/yyyy")%>'
                                                        runat="server" />
                                                </td>
                                                <td>
                                                    <asp:DataList ID="GetUSERCOMMENTS2" runat="server">
                                                        <HeaderTemplate>
                                                            <table>
                                                                <tr>
                                                                    <th>Product Id
                                                                    </th>                                                                
                                                                    <th>UnitPrice
                                                                    </th>
                                                                    <th>Quantity
                                                                    </th>
                                                                    <th>Discount
                                                                    </th>
                                                                </tr>
                                                        </HeaderTemplate>
                                                        <ItemTemplate>
                                                            <tr>
                                                                <td>
                                                                    <asp:Label ID="ProductId" Text='<%#Eval("ProductId")%>' runat="server" />
                                                                </td>
                                                                <td>
                                                                    <asp:Label ID="UnitPrice" Text='<%#Eval("UnitPrice")%>' runat="server" />
                                                                </td>
                                                                <td>
                                                                    <asp:Label ID="Quantity" Text='<%#Eval("Quantity")%>' runat="server" />
                                                                </td>
                                                                <td>
                                                                    <asp:Label ID="Discount" Text='<%#Eval("Discount")%>' runat="server" />
                                                                </td>
                                                        </ItemTemplate>
                                                        <FooterTemplate>
                                                            </table>
                                                        </FooterTemplate>
                                                    </asp:DataList>
                                                </td>
                                            </tr>
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            <asp:Label runat="server" Visible="<%#bool.Parse((dlCustomers.Items.Count==0).ToString())%>"
                                                Text="No Record Found!">
                                            </asp:Label>
                                            </table>
                                        </FooterTemplate>
                                    </asp:DataList>
                                </td>
                            </tr>
                        </ItemTemplate>
                        <FooterTemplate>
                            </table>
                        </FooterTemplate>
                    </asp:DataList>
                </div>

    In code behind:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    dlCustomers.DataSource = GetData("SELECT TOP 10 CustomerID,ContactName,City FROM Customers");
                    dlCustomers.DataBind();
                }
            }
    
            private static DataTable GetData(string query)
            {
                string constr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.CommandText = query;
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            using (DataSet ds = new DataSet())
                            {
                                DataTable dt = new DataTable();
                                sda.Fill(dt);
                                return dt;
                            }
                        }
                    }
                }
            }
    
            protected void dlCustomers_ItemDataBound(object sender, DataListItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    DataRowView dataRowView = e.Item.DataItem as DataRowView;
                    string customerId = dataRowView["CustomerID"].ToString();
                    DataList orderDataList = e.Item.FindControl("dlOrders") as DataList;
                    orderDataList.DataSource = GetData("SELECT TOP 5 OrderId,OrderDate FROM Orders WHERE CustomerID = '" + customerId + "'");
                    orderDataList.DataBind();
                }
            }
    
    
            protected void dlOrders_ItemDataBound(object sender, DataListItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    DataRowView dataRowView = e.Item.DataItem as DataRowView;
                    string Id = dataRowView["OrderId"].ToString();
                    DataList commentDataList = e.Item.FindControl("GetUSERCOMMENTS2") as DataList;
                    commentDataList.DataSource = GetData("SELECT TOP 5 ProductId, UnitPrice,Quantity,Discount FROM [Order Details] WHERE OrderID = '" + Id + "'");
                    commentDataList.DataBind();
                }
            }

    And the result is:

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 4, 2017 8:25 AM

All replies

  • User-1716253493 posted

    I guess almost no deferent, use nested datalist ItemDataBound

    Sunday, December 3, 2017 11:45 AM
  • User-2074858223 posted

    Nested Datalist C will be nested inside Nested B Datalist, that means Datalist B will be inside iDatalist A and Datalist C will be nested inside Datalist B

    Sunday, December 3, 2017 8:13 PM
  • User-1716253493 posted

    It's simply use B ItemDataBound to bind C DataList.

    Sunday, December 3, 2017 9:38 PM
  • User-2074858223 posted

    I did it didnt work, it was showing me Nested B Datalist data instead of Datalist C data

    protected void GetDatalistC_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
              DataRowView dataRowView = e.Item.DataItem as DataRowView;
                string Id = dataRowView["ID"].ToString();
                DataList commentDataList = e.Item.FindControl("GetUSERCOMMENTS2") as DataList;
                commentDataList.DataSource = GetData("SELECT TOP 5 TableID,UserName,Comments,FImageName,SendDate FROM PostComment WHERE ID = '" + Id + "'");
                commentDataList.DataBind();
    Sunday, December 3, 2017 10:39 PM
  • User-1716253493 posted

    AFAIK, Usualy nested DataList Data is filtered by parent DataList Item value

    DataList C inside DataList B, DataList B inside DataList A

    DataList B Data filtered by DataList A Item value, DataList C Data filtered by DataList B item value (or and also filtered by Item A value)

    Monday, December 4, 2017 12:45 AM
  • User-2074858223 posted

    Please can you illustrate that with a working example?

    Monday, December 4, 2017 5:56 AM
  • User-1716253493 posted

    Maybe DatalistCountry, DataListState and DataListCity

    DLCountry (County List)

    DLCountry ItemDataBound to populate nested DLState (select state yourtable  where country=@country)

    DLState ItemDataBount to populate nested DLCity (Select city from yourtable where state=@state)

    Monday, December 4, 2017 8:10 AM
  • User-335504541 posted

    <g class="gr_ gr_11 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="11" data-gr-id="11">Hi</g> micah2012,

    Please try to use the following code:

    In <g class="gr_ gr_35 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="35" data-gr-id="35">aspx</g>:

                <div>
                    <asp:DataList ID="dlCustomers" runat="server" OnItemDataBound="dlCustomers_ItemDataBound">
                        <HeaderTemplate>
                            <table>
                                <tr>
                                    <th>Name
                                    </th>
                                    <th>City
                                    </th>
                                    <th>Order
                                    </th>
                                </tr>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <tr>
                                <td>
                                    <asp:Label ID="lblContactName" runat="server" Text='<%#Eval("ContactName")%>'></asp:Label>
                                </td>
                                <td>
                                    <asp:Label ID="lblCity" runat="server" Text='<%#Eval("City")%>'></asp:Label>
                                </td>
                                <td>
                                    <asp:DataList ID="dlOrders" runat="server" OnItemDataBound="dlOrders_ItemDataBound">
                                        <HeaderTemplate>
                                            <table>
                                                <tr>
                                                    <th>Order Id
                                                    </th>
                                                    <th>Order Date
                                                    </th>
                                                    <th>Order Detail
                                                    </th>
                                                </tr>
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <tr>
                                                <td>
                                                    <asp:Label ID="lblOrderId" Text='<%#Eval("OrderId")%>' runat="server" />
                                                </td>
                                                <td>
                                                    <asp:Label ID="lblDate" Text='<%#Convert.ToDateTime(Eval("OrderDate")).ToString("dd/MM/yyyy")%>'
                                                        runat="server" />
                                                </td>
                                                <td>
                                                    <asp:DataList ID="GetUSERCOMMENTS2" runat="server">
                                                        <HeaderTemplate>
                                                            <table>
                                                                <tr>
                                                                    <th>Product Id
                                                                    </th>                                                                
                                                                    <th>UnitPrice
                                                                    </th>
                                                                    <th>Quantity
                                                                    </th>
                                                                    <th>Discount
                                                                    </th>
                                                                </tr>
                                                        </HeaderTemplate>
                                                        <ItemTemplate>
                                                            <tr>
                                                                <td>
                                                                    <asp:Label ID="ProductId" Text='<%#Eval("ProductId")%>' runat="server" />
                                                                </td>
                                                                <td>
                                                                    <asp:Label ID="UnitPrice" Text='<%#Eval("UnitPrice")%>' runat="server" />
                                                                </td>
                                                                <td>
                                                                    <asp:Label ID="Quantity" Text='<%#Eval("Quantity")%>' runat="server" />
                                                                </td>
                                                                <td>
                                                                    <asp:Label ID="Discount" Text='<%#Eval("Discount")%>' runat="server" />
                                                                </td>
                                                        </ItemTemplate>
                                                        <FooterTemplate>
                                                            </table>
                                                        </FooterTemplate>
                                                    </asp:DataList>
                                                </td>
                                            </tr>
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            <asp:Label runat="server" Visible="<%#bool.Parse((dlCustomers.Items.Count==0).ToString())%>"
                                                Text="No Record Found!">
                                            </asp:Label>
                                            </table>
                                        </FooterTemplate>
                                    </asp:DataList>
                                </td>
                            </tr>
                        </ItemTemplate>
                        <FooterTemplate>
                            </table>
                        </FooterTemplate>
                    </asp:DataList>
                </div>

    In code behind:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    dlCustomers.DataSource = GetData("SELECT TOP 10 CustomerID,ContactName,City FROM Customers");
                    dlCustomers.DataBind();
                }
            }
    
            private static DataTable GetData(string query)
            {
                string constr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.CommandText = query;
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            using (DataSet ds = new DataSet())
                            {
                                DataTable dt = new DataTable();
                                sda.Fill(dt);
                                return dt;
                            }
                        }
                    }
                }
            }
    
            protected void dlCustomers_ItemDataBound(object sender, DataListItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    DataRowView dataRowView = e.Item.DataItem as DataRowView;
                    string customerId = dataRowView["CustomerID"].ToString();
                    DataList orderDataList = e.Item.FindControl("dlOrders") as DataList;
                    orderDataList.DataSource = GetData("SELECT TOP 5 OrderId,OrderDate FROM Orders WHERE CustomerID = '" + customerId + "'");
                    orderDataList.DataBind();
                }
            }
    
    
            protected void dlOrders_ItemDataBound(object sender, DataListItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    DataRowView dataRowView = e.Item.DataItem as DataRowView;
                    string Id = dataRowView["OrderId"].ToString();
                    DataList commentDataList = e.Item.FindControl("GetUSERCOMMENTS2") as DataList;
                    commentDataList.DataSource = GetData("SELECT TOP 5 ProductId, UnitPrice,Quantity,Discount FROM [Order Details] WHERE OrderID = '" + Id + "'");
                    commentDataList.DataBind();
                }
            }

    And the result is:

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 4, 2017 8:25 AM