locked
Menu control in Asp.Net not working for third level RRS feed

  • Question

  • User751891957 posted

    Hi friends

                   I have to use a menu control in which three levels should be der like one category its subcategory and its subcategory in horizontal manner.I got two levels working .Third level menu data is coming along with second level menu.I have to separate this data and add as level 3 .

    I followed the following link for this problem-

    http://www.codeproject.com/KB/webforms/bindmenu.aspx

    aspx code-

    <div>
        <asp:Menu Width="165px" ID="Menu1" StaticMenuItemStyle-CssClass="menuItem" StaticHoverStyle-CssClass="menuHover"
        DynamicHoverStyle-CssClass="menuHover" DynamicMenuItemStyle-CssClass="menuItem"
        runat="server">
        <StaticItemTemplate>
            <%# Eval("Text") %>
            <asp:Label runat="server" ID="lbItmCount">[<%#Eval("ChildItems.Count")%>]</asp:Label>
        </StaticItemTemplate>
    </asp:Menu>
        </div>

    aspx.vb code-

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim obj As New dbs()
            If Not Page.IsPostBack Then
                BindMenu()
            End If
            Dim sq As String
            sq = "SELECT image FROM header_table where id = 1"
            obj.fillGrid(sq, grid_view2)
        End Sub
        Sub BindMenu()
            Dim connectionString As String = WebConfigurationManager.ConnectionStrings("DatabaseConnectionString1").ConnectionString
            Dim con As New MySqlConnection(connectionString)
            'Dim con As New SqlConnection(connectionString)
            Dim dadCategories As New MySqlDataAdapter("SELECT Category_id,Category_desc FROM category order by Category_desc", con)
            Dim dadSubCat As New MySqlDataAdapter("SELECT  SubCategory_id,Category_id,SubCategory_desc FROM subCategory order by SubCategory_desc", con)

            Dim dadsubpro As New MySqlDataAdapter("select sub_id,pid,SubProduct_desc from subproducts ", con)



            ' Add the DataTables to the DataSet
            Dim dsCat As New DataSet()
            Using con
                con.Open()

                dadSubCat.Fill(dsCat, "subCategory")
                dadCategories.Fill(dsCat, "category")
                dadsubpro.Fill(dsCat, "subproducts")

            End Using

            ' Add a DataRelation
            dsCat.Relations.Add("Children", dsCat.Tables("category").Columns("Category_id"), dsCat.Tables("subCategory").Columns("Category_id"))
            dsCat.Relations.Add("Childrens", dsCat.Tables("subCategory").Columns("SubCategory_id"), dsCat.Tables("subproducts").Columns("pid"))
            ' Add the Category nodes
            Dim count As Integer = 0
            For Each categoryRow As DataRow In dsCat.Tables("category").Rows


                Dim mNode As New MenuItem(CType(categoryRow("Category_desc"), String), "", "", "~/DetailView.aspx?CatID=" + CType(categoryRow("Category_id"), String), "_parent")
                Menu1.Items.Add(mNode)

                ' Get matching Sub Category
                Dim subCatRows() As DataRow = categoryRow.GetChildRows("Children")
                For Each row As DataRow In subCatRows
                    Dim subCatName As String = CType(row("SubCategory_desc"), String)
                    Dim subCatItems As New MenuItem(subCatName, "", "", "~/DetailView.aspx?CatID=" + CType(row("Category_id"), String) + "&SubCatID=" + CType(row("SubCategory_id"), String), "_parent")
                    Menu1.Items(count).ChildItems.Add(subCatItems)

                    ' Get matching Sub products
                    Dim countnew As Integer = 0
                    Dim subCatRows1() As DataRow = row.GetChildRows("Childrens")
                    For Each rows As DataRow In subCatRows1
                        Dim subpro As String = CType(rows("SubProduct_desc"), String)
                        Dim subProItems As New MenuItem(subpro, "", "", "~/DetailView.aspx?SubCatID=" + CType(rows("sub_id"), String) + "&SubProID=" + CType(rows("sub_id"), String), "_parent")
                        Menu1.Items(countnew).ChildItems.Add(subProItems)


                    Next

                    countnew = countnew + 1

                Next
                count = count + 1
            Next
        End Sub

    End Class


    A solution is urgent...

    Wednesday, October 14, 2009 10:08 AM

Answers

All replies

  • User-598822553 posted

    Instead of binding the menu through code, I would prefer using XML as a datasource. You can even transform your dataset to an xml, and bind it to your menu. This eliminates writing loops to bind. Just a thought, you might want to look at this sample.

    http://www.aspdotnetcodes.com/Asp.Net_Menu_Control_XmlDataSource.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 14, 2009 5:14 PM
  • User-2115483147 posted

    Please refer this article, and you can download the code on the the article,

    Building a Database Driven Hierarchical Menu using ASP.NET 2.0



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 19, 2009 2:09 AM
  • User421793344 posted

    Hi 

    Sorry I am indulging in existing issue which has been marked as "Answerd Solution"  for above issue. Well i found one of other alternative for the new user who is refering to above thread.

    ASPX Design:

     <asp:Menu ID="Menu" CssClass="TopGroup" runat="server" Orientation="Horizontal" DynamicMenuItemStyle-CssClass="Level2Item"
                StaticMenuItemStyle-CssClass="TopItem" StaticMenuItemStyle-ItemSpacing="1" StaticEnableDefaultPopOutImage="false"
                StaticMenuItemStyle-HorizontalPadding="10" StaticMenuItemStyle-VerticalPadding="3"
                DynamicMenuItemStyle-HorizontalPadding="20" DynamicMenuItemStyle-VerticalPadding="2"
                DynamicMenuStyle-CssClass="Level2Item">
            </asp:Menu>
    

    Code Behind:

     protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
    
                DataSet ds = new DataSet();
                //Fill the dataset from Backend 
                ds= FillMenu();
                if (ds != null && ds.Tables.Count > 0)
                {
    
                    DataView dv = new DataView(ds.Tables[0], "parentid is null", "", DataViewRowState.CurrentRows);
                    for (int i = 0; i < dv.Count; i++)
                    {
                        string menucode = dv.ToTable().Rows[i]["menuid"].ToString();
                        MenuItem mparent = new MenuItem();
                        mparent.Text = dv.ToTable().Rows[i]["menutext"].ToString();
                        mparent.Value = dv.ToTable().Rows[i]["menuid"].ToString();
                        if (!string.IsNullOrEmpty(dv.ToTable().Rows[i]["navigateurl"].ToString()))
                            mparent.NavigateUrl = dv.ToTable().Rows[i]["navigateurl"].ToString();
    
                        DataView dvChild = new DataView(ds.Tables[0], "parentid=" + menucode + "", "", DataViewRowState.CurrentRows);
                        for (int j = 0; j < dvChild.Count; j++)
                        {
                            MenuItem mchild = new MenuItem();
                            mchild.Text = dvChild.ToTable().Rows[j]["menutext"].ToString();
                            mchild.Value = dvChild.ToTable().Rows[j]["menuid"].ToString();
                            mchild.NavigateUrl = dvChild.ToTable().Rows[j]["navigateurl"].ToString();
    
                            DataView dvSubChild = new DataView(ds.Tables[0], "parentid=" + mchild.Value + "", "", DataViewRowState.CurrentRows);
                            for (int k = 0; k < dvSubChild.Count; k++)
                            {
                                MenuItem msubchild = new MenuItem();
                                msubchild.Text = dvSubChild.ToTable().Rows[k]["menutext"].ToString();
                                msubchild.Value = dvSubChild.ToTable().Rows[k]["menuid"].ToString();
                                msubchild.NavigateUrl = dvSubChild.ToTable().Rows[k]["navigateurl"].ToString();
                                mchild.ChildItems.Add(msubchild);
                            }
                            mparent.ChildItems.Add(mchild);
                        }
                        Menu.Items.Add(mparent);
                    }
                }
            }
        }
    

    Database design:

    MenuID		MenuText	            ParentID	               NavigateURL
    1		Home				~/Home.aspx
    2		ASP		1		
    3		Java		1		~/Java.aspx
    4		ClassicASP	2		~/ClassicASP.aspx
    5		ASPDotNEt	2		~/ASPDotNEt.aspx
    6		Logout				~/Logout.aspx



    From above Table structure following Menu will be dispalyed:

    Home				Logout
    ASP      >	ClassicASP
    	ASPDotNEt
    Java



     

    Thursday, October 7, 2010 3:29 AM
  • User944339287 posted

    Hi,

    Mind to tell me how to FillMenu? (sql statement pls... tq)

    Thursday, October 24, 2013 11:52 PM