动态菜单生成.[来自数据库] [英] Dynamic menu generation.[from database]

查看:85
本文介绍了动态菜单生成.[来自数据库]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我有一个需要动态菜单生成的应用程序. 我可以在运行时从数据库添加菜单项的地方.


预先感谢.

Hello friends,

I have an application which requires Dynamic menu generation. Where i can add menu items from database at runtime.


Thanks in advance.

推荐答案

您可以将数据源绑定到Menu控件,仅此而已.

在ASP.NET 2.0中填充菜单控件-使用不同的数据源 [
You can bind the datasource to Menu control, That''s all.

Populating Menu Control in ASP.NET 2.0 - using different data sources[^]


如果要添加菜单您可以自行创建菜单表,该子菜单具有父menuid字段,并且该字段中的parent为null.并在pageload中使用类似以下代码的代码:
If you want to add menu items by your self, you can make a menu table, that submenus has parent menuid field and parents be null in this field. and use something like this code in pageload :
void BindParentMenu()
   {
       try
       {

           Menu1.Items.Clear();

           //Add all items with null parentID
           SqlDataReader sdr = df.Get_All_ParentMainMenu();
           while (sdr.Read())
           {
               MenuItem mBase = new MenuItem();
               mBase.Value = sdr["MenuID"].ToString();
               mBase.Text = sdr["Title"].ToString();
               mBase.NavigateUrl = sdr["Url"].ToString();
               if (sdr["Url"].ToString() == "#")
               {
                   mBase.NavigateUrl = Request.Url.AbsoluteUri + "#";
               }
               SubMenu(mBase);
               Menu1.Items.Add(mBase);
                           }
           sdr.Close();
       }
       catch (Exception)
       {
       }
   }

  void SubMenu(MenuItem mBase)
   {
       try
       {
           //All items that their parent is mBase
           SqlDataReader sdr = df.Get_All_Menu_Child(Convert.ToInt32(mBase.Value));
           while (sdr.Read())
           {
               MenuItem mChild = new MenuItem();
               mChild.Value = sdr["MenuID"].ToString();
               mChild.Text = sdr["Title"].ToString();
               mChild.NavigateUrl = sdr["Url"].ToString();
               if (sdr["Url"].ToString() == "#")
               {
                   mChild.NavigateUrl = Request.Url.AbsoluteUri + "#";
               }
               mBase.ChildItems.Add(mChild);
               SubMenu(mChild);
           }
           sdr.Close();
       }
       catch (Exception)
       {
       }
   }


您必须确保数据库表能够提供完整配置菜单所必需的信息,这意味着您不仅需要了解和MenuItem类.我对如何执行操作有自己的想法,但是这值得写一篇文章,而且内容太多,无法快速回答.

除此以外,您还可以在Google上找到Google.这是我使用搜索词"C#动态创建菜单"找到的Codeproject文章:

C#创建动态菜单 [
You have to make sure your database table is capable of providing the info necessary to fully configure the menu, which means you need more than just a cursory knowledge of the and MenuItem class. I have my own ideas about how to do it, but it would be worthy of an article, and much too extensive to fit into a quick answer.

Beyond that Google is where you want to look. Here''s a Codeproject article I found using the search term "C# create menu dynamically":

C# Creating Dynamic Menus[^]


这篇关于动态菜单生成.[来自数据库]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆