简体   繁体   中英

Bulding Menu with asp.net using the Database

I have an table in database and I need create Menu in my project using this database.

How I create this with asp.net c#? I need transform this line "N:\\A\\B\\C" in menu style Tree View. But this menu should be dynamic, because in this table, the paths may have varying levels of folders! For each row in my table, one Main Menu, and for each subfolder, one MenuChild.

Ex: "N:\\A" is my Main Menu and expands to "B\\" that is my second level (submenu) and expands to "C\\" that is my third level in menu

I Create the first level for this menu. This show the full path for each line in table.

private DataTable GetMenuData()
{
    string selectCommand = "SELECT distinct folder_path FROM A";
    string conString = ConfigurationManager.ConnectionStrings["A"].ConnectionString;
    SqlDataAdapter dad = new SqlDataAdapter(selectCommand, conString);
    DataTable dtblCategories = new DataTable();
    dad.Fill(dtblCategories);
    return dtblCategories;
}

private void AddTopMenuItems(DataTable menuData)
{
    DataView view = new DataView(menuData);
    foreach (DataRowView row in view)
    {
        MenuItem newMenuItem = new MenuItem(row["folder_path"].ToString(), row["folder_path"].ToString());
        Menu1.Items.Add(newMenuItem);
    }
}

<div id="WorkingZone"> <asp:Menu ID="Menu1" Orientation="vertical" StaticMenuItemStyleCssClass="menuItem" DynamicMenuItemStyle-CssClass="menuItem" runat="server" /> </div>

You can not achieve this with this table structure. you have to change table structure. you have to add a column for parentid which will tell you that node is parent or child.

please see below link

http://www.codeproject.com/Tips/354696/Dynamically-populating-menu-items-from-the-databas

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM