久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

asp.net 數(shù)據(jù)綁定菜單多級

asp.net databound menu multilevel(asp.net 數(shù)據(jù)綁定菜單多級)
本文介紹了asp.net 數(shù)據(jù)綁定菜單多級的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我目前正在使用 asp.net 菜單控件從表父/子項加載.我遇到的問題是,如果孩子有另一個孩子.從這個意義上說,我的代碼有點靜態(tài),我似乎找不到更好的或該"的方法來做到這一點.我已經(jīng)將站點地圖視為數(shù)據(jù)源,但我不需要站點地圖,并且覺得這對于我需要實現(xiàn)的目標(biāo)來說太過分了.

I am currently using an asp.net menu control to load from a table parent/child items. The problem I am having is that if the child has another child. My code is kindof static in that sense and I can't seem to find a better or "the" way to do it. I have seen sitemap as datasources but i don't need a sitemap and feel that would just be overkill for what I need to achieve.

foreach (ClassName option in list)
{
   MenuItem module = new MenuItem(option.Description.ToLower(), "", "", option.Url + "?option=" + option.Optionid);
   module.Selectable = true;
   navigation.Items.Add(module);
   //this is my second level
   foreach (ClassName child in listfromparent(option.Optionid))
   {
         MenuItem childmenu = new MenuItem(child.Description.ToLower(), "", "", child.Url + "?option=" + child.Optionid);
         module.ChildItems.Add(childmenu);
   }
 }

如您所見,這適用于 2 個級別:(當(dāng)然,我可以在 child 中放置另一個 childlevel 來創(chuàng)建第 3 個級別,但是如果有第 4 個、第 5 個呢?所以這就是為什么我需要它自己做.我注意到 treeview 有 onpopulate 但顯然 Menu 沒有.提前致謝.

as you can see this works but for 2 levels :( and of course i could put another childlevel inside child to create the 3rd level but what if there is a 4th, 5th? So that's why I need it to do it itself. I noticed treeview has onpopulate but apparently Menu doesn't. Thanks in advance.

推薦答案

這是您可以做到的一種方法.

Here's one way you could do it.

  • 用鄰接表表示表中的父/子關(guān)系
  • 將該鄰接表映射為樹狀結(jié)構(gòu)
  • 將該樹結(jié)構(gòu)轉(zhuǎn)換為您的菜單項結(jié)構(gòu)

也許您可以跳過中間步驟,將鄰接列表直接映射到 MenuItems 樹,也許可以在 MenuItem 上使用一些擴展方法.

Maybe you could skip that middle step and map the adjacency list straight to a tree of MenuItems, maybe with some extension methods on MenuItem.

但無論如何...

Default.aspx

<%@ Page Language="C#" Inherits="MenuTreeDemo.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head runat="server">
    <title>Default</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Menu ID="MyMenu" runat="server" StaticDisplayLevels="3" />
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections.Generic;

namespace MenuTreeDemo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)    
        {
            if (!IsPostBack)
            {
                MenuNode root = ConvertTableToTree(GetTreeTable());
                foreach (MenuNode topLevelNode in root.Children)
                {
                    MyMenu.Items.Add(topLevelNode.ToMenuItem()); // Visits all nodes in the tree.
                }
            }
        }


        // The menu tree as an adjacency list in a table. 
        static DataTable GetTreeTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("Id", typeof(int));
            table.Columns.Add("Description", typeof(string));
            table.Columns.Add("Url", typeof(string));
            table.Columns.Add("ParentId", typeof(int));

            table.Rows.Add(1, "TopMenu1", "/foo.html", 0);
            table.Rows.Add(2, "SubMenu1.1", "/baz.html", 1);
            table.Rows.Add(3, "SubMenu1.2", "/barry.html", 1);
            table.Rows.Add(4, "SubMenu1.2.1", "/skeet.html", 3);
            table.Rows.Add(5, "TopMenu2", "/bar.html", 0);
            table.Rows.Add(6, "TopMenu3", "/bar.html", 0);
            table.Rows.Add(7, "SubMenu3.1", "/ack.html", 6);

            return table;
        }


        // See e.g. http://stackoverflow.com/questions/2654627/most-efficient-way-of-creating-tree-from-adjacency-list
        // Assuming table is ordered.
        static MenuNode ConvertTableToTree(DataTable table)
        {
            var map = new Dictionary<int, MenuNode>();
            map[0] = new MenuNode() { Id = 0 }; // root node

            foreach (DataRow row in table.Rows)
            {
                int nodeId = int.Parse(row["Id"].ToString());
                int parentId = int.Parse(row["ParentId"].ToString());

                MenuNode newNode = MenuNodeFromDataRow(row);

                map[parentId].Children.Add(newNode);
                map[nodeId] = newNode;
            }

            return map[0]; // root node
        }


        static MenuNode MenuNodeFromDataRow(DataRow row)
        {
            int nodeId = int.Parse(row["Id"].ToString());
            int parentId = int.Parse(row["ParentId"].ToString());
            string description = row["Description"].ToString();
            string url = row["Url"].ToString();

            return new MenuNode() { Id=nodeId, ParentId=parentId, Description=description, Url=url };
        }
    }
}

MenuNode.cs

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace MenuTreeDemo
{
    public class MenuNode
    {
        public int Id { get; set; }
        public int ParentId { get; set; }
        public string Description { get; set; }
        public string Url { get; set; }
        public List<MenuNode> Children { get; set; }


        public MenuNode ()
        {
            Children = new List<MenuNode>();
        }


        // Will visit all descendants and turn them into menu items.
        public MenuItem ToMenuItem()
        {
            MenuItem item = new MenuItem(Description) { NavigateUrl=Url };
            foreach (MenuNode child in Children)
            {
                item.ChildItems.Add(child.ToMenuItem());
            }

            return item;
        }
    }
}

這篇關(guān)于asp.net 數(shù)據(jù)綁定菜單多級的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

LINQ to SQL and Concurrency Issues(LINQ to SQL 和并發(fā)問題)
Yield return from a try/catch block(try/catch 塊的收益回報)
Should I call Parameters.Clear when reusing a SqlCommand with a transation?(重用帶有事務(wù)的 SqlCommand 時,我應(yīng)該調(diào)用 Parameters.Clear 嗎?)
what does a using statement without variable do when disposing?(處理時不帶變量的 using 語句有什么作用?)
Why doesn#39;t TransactionScope work with Entity Framework?(為什么 TransactionScope 不適用于實體框架?)
How to dispose TransactionScope in cancelable async/await?(如何在可取消的 async/await 中處理 TransactionScope?)
主站蜘蛛池模板: 欧美日韩免费一区二区三区 | 香蕉视频导航 | 免费看黄色aaaaaa 片 | www黄色| a毛片视频| av免费网站 | 成年免费视频黄网站在线观看 | 国产精品99久久久久久久久 | 国产天天操 | 国产原创精品 | 精品一区二区三区在线观看 | 成人高潮片免费视频 | 黄色免费大片 | 国产成人一区二区三区 | 国产寡妇亲子伦一区二区三区四区 | 在线观看av的网站 | 色综合久久天天综合网 | www.一区二区 | 午夜免费福利视频 | 日韩免费一区二区三区 | 欧美一级精品 | 国产伦精品一区二区三区视频我 | 国产欧美一区二区精品性色超碰 | 一区二区国产视频 | 欧美性猛交乱大交 | 日本一本在线 | 久久a视频 | 毛片毛片毛片毛片毛片毛片 | 天天躁日日躁狠狠很躁 | 黄色欧美大片 | 欧美bbb| 女人高潮特级毛片 | 亚洲一区在线观看视频 | 人人干人人艹 | 干干干操操操 | 9l视频自拍九色9l视频成人 | 国产在线成人 | 日本不卡视频在线观看 | 欧美第一页 | 亚洲专区一区 | 久久久久婷婷 |