Bind (Populate) N-Level nested Repeater using C# and VB.Net in ASP.Net

Last Reply 24 days ago By dharmendr

Posted 24 days ago

Dear Team,

I need help regarding binding of Nested Repeater.

Repeter RptrMyMenuSide & RptrMyMenuSideSub binding successfully.

but RptrMyMenuSideSubSub giving error object not reference not able to find control, beow is the code,

please help if i doing something wrong.

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            CategoriesSideMenu()
            ProductSideMenuSub()
            ProductSideMenuSubSub()
        End If
    End Sub
    Public Sub CategoriesSideMenu()
        RptrMyMenuSide.Visible = True
        cls.bindrepeter(RptrMyMenuSide, "Select CATID,CATNAME from CATEGORY ORDER BY CATORDER")

    End Sub
    Public Sub ProductSideMenuSub()
        For i As Integer = 0 To RptrMyMenuSide.Items.Count - 1
            Dim id As Label = DirectCast(RptrMyMenuSide.Items(i).FindControl("lblcatid"), Label)
            Dim RptrMyMenuSideSub As Repeater = DirectCast(RptrMyMenuSide.Items(i).FindControl("RptrMyMenuSideSub"), Repeater)
            cls.bindrepeter(RptrMyMenuSideSub, "Select SubCatId,SubCatName from SubCategoryMaster  WHERE CategoryId=" & id.Text & " ORDER BY SubCatOrder")
        Next
    End Sub
    Public Sub ProductSideMenuSubSub()
        For i As Integer = 0 To RptrMyMenuSide.Items.Count - 1
            Dim id As Label = DirectCast(RptrMyMenuSide.Items(i).FindControl("lblsubcatid"), Label)
            Dim RptrMyMenuSideSubSub As Repeater = DirectCast(RptrMyMenuSide.Items(i).FindControl("RptrMyMenuSide_RptrMyMenuSideSub"), Repeater)
            cls.bindrepeter(RptrMyMenuSideSubSub, "Select PRODUCTID,PRODUCTNAME from PRODUCT  WHERE PRODUCTCATID=" & id.Text & " ORDER BY PRODUCTCATID,PRODUCTNAME")
        Next
    End Sub
    Public Function bindrepeter(ByVal objRepeater As Object, ByVal Qry As String) As DataSet
            Dim con As New SqlConnection
            con.ConnectionString = ConfigurationManager.ConnectionStrings("conString").ConnectionString
            Dim ds As DataSet
            Dim adp As New SqlDataAdapter(Qry, con)
            ds = New DataSet()
            adp.Fill(ds)
            objRepeater.DataSource = ds
            objRepeater.DataBind()
            con.Close()
            Return ds
        End Function

 

<asp:Repeater ID="RptrMyMenuSide" runat="server" EnableTheming="True">
    <ItemTemplate>
        <asp:Label ID="lblcatid" runat="server" Text='<%# Eval("CATID") %>' Visible="true"></asp:Label>
        <li id="menu-item-<%# Eval("CATID")%>" class="menu-item-<%# Eval("CATID")%>"><a>
            <%# Eval("CATNAME")%></a>
            <asp:Repeater ID="RptrMyMenuSideSub" runat="server" EnableTheming="True">
                <ItemTemplate>
                    <ul class="sub-menu">
                        <asp:Label ID="lblsubcatid" runat="server" Text='<%# Eval("SubCatId") %>' Visible="true"></asp:Label>
                        <li id="menu-item-<%# Eval("SubCatId") %>" class="menu-item-<%# Eval("SubCatId") %>">
                            <a href="index.html">
                                <%# Eval("SubCatName")%></a>
                            <asp:Repeater ID="RptrMyMenuSideSubSub" runat="server" EnableTheming="True">
                                <ItemTemplate>
                                    <ul class="sub-menu">
                                        <asp:Label ID="lblprodid" runat="server" Text='<%# Eval("PRODUCTID") %>' Visible="false"></asp:Label>
                                        <li id="menu-item-<%# Eval("PRODUCTID") %>" class="menu-item-<%# Eval("PRODUCTID") %>">
                                            <a href="index.html">
                                                <%# Eval("PRODUCTNAME")%></a> </li>
                                    </ul>
                                </ItemTemplate>
                            </asp:Repeater>
                        </li>
                    </ul>
                </ItemTemplate>
            </asp:Repeater>
        </li>
    </ItemTemplate>
</asp:Repeater>

 

 

Posted 24 days ago

Hi eshant.kapoor,

Check this example. Now please take its reference and correct your code.

For this example i have used dynamic datatable and folter record using select method to bind the Repeater. You need to replace with database binding code.

HTML

<asp:Repeater ID="RptrMyMenuSide" runat="server" EnableTheming="True">
    <ItemTemplate>
        <asp:Label ID="lblcatid" runat="server" Text='<%# Eval("CATID") %>' Visible="true"></asp:Label>
        <li id="menu-item-<%# Eval("CATID")%>" class="menu-item-<%# Eval("CATID")%>"><a>
            <%# Eval("CATNAME")%></a>
            <asp:Repeater ID="RptrMyMenuSideSub" runat="server" EnableTheming="True">
                <ItemTemplate>
                    <ul class="sub-menu">
                        <asp:Label ID="lblsubcatid" runat="server" Text='<%# Eval("SubCatId") %>' Visible="true"></asp:Label>
                        <li id="menu-item-<%# Eval("SubCatId") %>" class="menu-item-<%# Eval("SubCatId") %>">
                            <a href="index.html">
                                <%# Eval("SubCatName")%></a>
                            <asp:Repeater ID="RptrMyMenuSideSubSub" runat="server" EnableTheming="True">
                                <ItemTemplate>
                                    <ul class="sub-menu">
                                        <asp:Label ID="lblprodid" runat="server" Text='<%# Eval("PRODUCTID") %>' Visible="false"></asp:Label>
                                        <li id="menu-item-<%# Eval("PRODUCTID") %>" class="menu-item-<%# Eval("PRODUCTID") %>">
                                            <a href="index.html">
                                                <%# Eval("PRODUCTNAME")%></a> </li>
                                    </ul>
                                </ItemTemplate>
                            </asp:Repeater>
                        </li>
                    </ul>
                </ItemTemplate>
            </asp:Repeater>
        </li>
    </ItemTemplate>
</asp:Repeater>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, System.EventArgs e)
{
    if (!IsPostBack)
    {
        CategoriesSideMenu();
        ProductSideMenuSub();
    }
}

public void CategoriesSideMenu()
{
    RptrMyMenuSide.Visible = true;
    DataTable dt = GetCategory();
    RptrMyMenuSide.DataSource = dt;
    RptrMyMenuSide.DataBind();
}

public void ProductSideMenuSub()
{
    for (int i = 0; i <= RptrMyMenuSide.Items.Count - 1; i++)
    {
        Label id = (Label)RptrMyMenuSide.Items[i].FindControl("lblcatid");
        Repeater RptrMyMenuSideSub = (Repeater)RptrMyMenuSide.Items[i].FindControl("RptrMyMenuSideSub");
        DataTable dt = GetSubCategoryMaster();
        RptrMyMenuSideSub.DataSource = dt.Select("CategoryId=" + id.Text).CopyToDataTable();
        RptrMyMenuSideSub.DataBind();
        ProductSideMenuSubSub(RptrMyMenuSideSub);
    }
}

public void ProductSideMenuSubSub(Repeater RptrMyMenuSideSub)
{
    for (int i = 0; i <= RptrMyMenuSideSub.Items.Count - 1; i++)
    {
        Label id = (Label)RptrMyMenuSideSub.Items[i].FindControl("lblsubcatid");
        Repeater RptrMyMenuSideSubSub = (Repeater)RptrMyMenuSideSub.Items[i].FindControl("RptrMyMenuSideSubSub");
        DataTable dt = GetProduct();
        RptrMyMenuSideSubSub.DataSource = dt.Select("PRODUCTCATID=" + id.Text).CopyToDataTable();
        RptrMyMenuSideSubSub.DataBind();
    }
}

private static DataTable GetCategory()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { 
                        new DataColumn("CATID", typeof(int)),
                        new DataColumn("CATNAME", typeof(string)) });
    dt.Rows.Add(1, "Cat 1");
    dt.Rows.Add(2, "Cat 2");
    dt.Rows.Add(3, "Cat 3");
    dt.Rows.Add(4, "Cat 4");
    return dt;
}

private static DataTable GetSubCategoryMaster()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { 
                        new DataColumn("SubCatId", typeof(int)),
                        new DataColumn("SubCatName", typeof(string)),
                        new DataColumn("CategoryId", typeof(int)) });
    dt.Rows.Add(1, "Sub Cat 1", 1);
    dt.Rows.Add(2, "Sub Cat 2", 2);
    dt.Rows.Add(3, "Sub Cat 3", 4);
    dt.Rows.Add(4, "Sub Cat 4", 3);
    dt.Rows.Add(5, "Sub Cat 5", 1);
    return dt;
}

private static DataTable GetProduct()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { 
                        new DataColumn("PRODUCTID", typeof(int)),
                        new DataColumn("PRODUCTNAME", typeof(string)),
                        new DataColumn("PRODUCTCATID", typeof(string)) });
    dt.Rows.Add(1, "Product 1", 1);
    dt.Rows.Add(2, "Product 2", 3);
    dt.Rows.Add(3, "Product 3", 5);
    dt.Rows.Add(4, "Product 4", 2);
    dt.Rows.Add(5, "Product 5", 4);
    dt.Rows.Add(6, "Product 6", 4);
    return dt;
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        CategoriesSideMenu()
        ProductSideMenuSub()
    End If
End Sub

Public Sub CategoriesSideMenu()
    RptrMyMenuSide.Visible = True
    Dim dt As DataTable = GetCategory()
    RptrMyMenuSide.DataSource = dt
    RptrMyMenuSide.DataBind()
End Sub

Public Sub ProductSideMenuSub()
    For i As Integer = 0 To RptrMyMenuSide.Items.Count - 1
        Dim id As Label = CType(RptrMyMenuSide.Items(i).FindControl("lblcatid"), Label)
        Dim RptrMyMenuSideSub As Repeater = CType(RptrMyMenuSide.Items(i).FindControl("RptrMyMenuSideSub"), Repeater)
        Dim dt As DataTable = GetSubCategoryMaster()
        RptrMyMenuSideSub.DataSource = dt.[Select]("CategoryId=" & id.Text).CopyToDataTable()
        RptrMyMenuSideSub.DataBind()
        ProductSideMenuSubSub(RptrMyMenuSideSub)
    Next
End Sub

Public Sub ProductSideMenuSubSub(ByVal RptrMyMenuSideSub As Repeater)
    For i As Integer = 0 To RptrMyMenuSideSub.Items.Count - 1
        Dim id As Label = CType(RptrMyMenuSideSub.Items(i).FindControl("lblsubcatid"), Label)
        Dim RptrMyMenuSideSubSub As Repeater = CType(RptrMyMenuSideSub.Items(i).FindControl("RptrMyMenuSideSubSub"), Repeater)
        Dim dt As DataTable = GetProduct()
        RptrMyMenuSideSubSub.DataSource = dt.[Select]("PRODUCTCATID=" & id.Text).CopyToDataTable()
        RptrMyMenuSideSubSub.DataBind()
    Next
End Sub

Private Shared Function GetCategory() As DataTable
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("CATID", GetType(Integer)),
                                          New DataColumn("CATNAME", GetType(String))})
    dt.Rows.Add(1, "Cat 1")
    dt.Rows.Add(2, "Cat 2")
    dt.Rows.Add(3, "Cat 3")
    dt.Rows.Add(4, "Cat 4")
    Return dt
End Function

Private Shared Function GetSubCategoryMaster() As DataTable
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("SubCatId", GetType(Integer)),
                                          New DataColumn("SubCatName", GetType(String)),
                                          New DataColumn("CategoryId", GetType(Integer))})
    dt.Rows.Add(1, "Sub Cat 1", 1)
    dt.Rows.Add(2, "Sub Cat 2", 2)
    dt.Rows.Add(3, "Sub Cat 3", 4)
    dt.Rows.Add(4, "Sub Cat 4", 3)
    dt.Rows.Add(5, "Sub Cat 5", 1)
    Return dt
End Function

Private Shared Function GetProduct() As DataTable
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("PRODUCTID", GetType(Integer)),
                                          New DataColumn("PRODUCTNAME", GetType(String)),
                                          New DataColumn("PRODUCTCATID", GetType(String))})
    dt.Rows.Add(1, "Product 1", 1)
    dt.Rows.Add(2, "Product 2", 3)
    dt.Rows.Add(3, "Product 3", 5)
    dt.Rows.Add(4, "Product 4", 2)
    dt.Rows.Add(5, "Product 5", 4)
    dt.Rows.Add(6, "Product 6", 4)
    Return dt
End Function

Screenshot