代码之家  ›  专栏  ›  技术社区  ›  Ahmer Ali Ahsan

如何在ASP.Net中使用递归生成动态菜单树?

  •  1
  • Ahmer Ali Ahsan  · 技术社区  · 7 年前

    假设我脑海中有下面的菜单图片。

    enter image description here

    根据上面的图片,我创建了我的SQL CTE查询和 返回以下结果。

    enter image description here

    脚本

    我试过什么了?

    public string recurseMenu(string ID = "0")
        {
            string s = "<ul id='ah-menu-level1'>";
            foreach (DataRow row in dtNavigation.Tables[0].Rows)
            {
                if (row["Root_Navigation_ID"].ToString() == ID)
                {
                    s += "<li><a class='ah-anchor-tooltip-show' href='javascript:void(0)'> <i class='fa " + row["CSS_Class"].ToString() + " fa-lg' aria-hidden='true'></i></a><ul class='sub-menu'><li><a href='javascript:void(0)'><strong>" + row["Name"].ToString() + "</strong></a></li>";
    
                    if (row["ID"].ToString() != "0")
                    {
                        s += recurseMenu(row["ID"].ToString());
                    }
                    s += "</ul>";
                }
            }
            return s += "</ul>";
        }
    

    站点.master

    <% if (dtNavigation.Tables.Count > 0){%>
    <%= recurseMenu() %>
    <%}%>
    

    上面的代码没有显示出所需的结果。它正确地显示了第一级菜单,但没有显示子菜单。

    我在代码中做错了什么我找不到。请帮帮我。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Ahmer Ali Ahsan    6 年前

    我相信我已经解决了这一切,所以我会回答我自己的问题。看看下面的代码。如果有其他关于优化的修改,请告诉我。

    输出

    enter image description here

    站点.master.cs

    public string AddTopMenuItems(DataTable dt)
        {
            string dHTML = "";
            DataView DataView = new DataView(dt);
            DataView.RowFilter = "Root_Navigation_ID = 0";
            foreach (DataRowView GetParentRow in DataView) //Filter and Iterate on those rows in which root id is zero.
            {
                dHTML += "<li><a class='ah-anchor-tooltip-show' href='javascript:void(0)'><i class='fa " + GetParentRow["CSS_Class"].ToString() + " fa-lg' aria-hidden='true'></i></a>";
                dHTML += "<ul class='sub-menu'><li><a href='javascript:void(0)'><strong>" + GetParentRow["Name"] + "</strong></a>";
                NavigationItem item = new NavigationItem(GetParentRow["ID"].ToString(), GetParentRow["Root_Navigation_ID"].ToString(), GetParentRow["Name"].ToString());
                dHTML += AddChildMenuItems(dt, item);
                dHTML += "</li></ul></li>";
            }
            return dHTML;
        }
    
        private string AddChildMenuItems(DataTable dt, NavigationItem parentMenu)
        {
            EnumerableRowCollection<DataRow> GetChildrens = from getChilds in dtNavigation.Tables[0].AsEnumerable()
                                                            where getChilds.Field<Int64>("Root_Navigation_ID") == Convert.ToInt64(parentMenu.ID.ToString())
                                                            select getChilds; 
            DataView childs = GetChildrens.AsDataView();
    
            string retval = "";
            foreach (DataRowView child in childs)
            {
                NavigationItem subMenu = new NavigationItem(child["ID"].ToString(), child["Root_Navigation_ID"].ToString(), child["Name"].ToString());
                retval += "<ul><li><a href='javascript:void(0)'><strong>" + child["Name"].ToString().ToString() + "</strong></a>";
                retval += this.AddChildMenuItems(dt, subMenu);
                retval += "</li></ul>";
            }
            return retval;
        }
    

    <% if (dtNavigation.Tables.Count > 0)
        { 
    %>
            <ul id="menu">
            <%= AddTopMenuItems(dtNavigation.Tables[0]) %>
            </ul>
    <% 
        } 
    %>