在 ASP.NET 中动态添加/删除菜单项?

发布于 2024-11-02 22:51:21 字数 92 浏览 4 评论 0原文

我有一个菜单控件(Menu1),我想根据我在数据库中存储的有关经过身份验证的用户的某些信息从菜单中添加/删除项目。我不确定如何从菜单控件访问特定菜单项并在运行时删除它们?

I have a menu control (Menu1) and I want to add/remove items from the menu based on certain information I have stored about the authenticated user in the database. I'm not sure though how to access specific menu items from the menu control and remove them at runtime?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝影如岚 2024-11-09 22:51:21

ASP.NET 菜单可以通过隐藏代码访问。例如,在标记中声明的菜单具有 id“Menu1”,可以像这样访问:

  foreach (MenuItem item in Menu1.Items) { 
    if (item.NavigateUrl.Contains(pageName)) {
       item.Selected = true;
       item.Text = "customText";  
    }
    // ... 
  }

在该示例中,根据菜单所在的当前页面选择当前选定的菜单项。同样,Items 集合可用于添加或删除单个菜单项。
请注意,在菜单项上,ChildItems 集合可用于更改子菜单项集合。

更多信息:http://msdn。 microsoft.com/en-us/library/system.web.ui.webcontrols.menu.items.aspx

@Edit:使其与问题中的数据更加一致

ASP.NET menus can be accessed via code behind. A menu declared in the markup, having the id 'Menu1' for example, could be accessed like so:

  foreach (MenuItem item in Menu1.Items) { 
    if (item.NavigateUrl.Contains(pageName)) {
       item.Selected = true;
       item.Text = "customText";  
    }
    // ... 
  }

In that example, the currently selected menu item is chosen according to the current page the menu is on. Just as well, the Items collection may be used to add or remove single menu items.
Note, on menu items, the ChildItems collection can be used to alter the submenu items collection.

More infos: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.menu.items.aspx

@Edit: made it more coherent with the data in the question

北笙凉宸 2024-11-09 22:51:21

我希望你能说得更具体,但我知道评论通知在这里有点糟糕。那么,如果您能确切地告诉我

  1. 您将根据什么信息添加/删除项目?
  2. 它们如何存储在数据库中(这意味着它们是存储在成员资格表之一中?在个人资料表中?还是在其中引用用户的自定义表中?
  3. 您用简单的英语想象的技术是什么?

我不确定如何访问
菜单中的特定菜单项
在运行时控制并删除它们?

  • A.删除项目:

    Menu1.Items.Remove(Menu1.FindItem("作业"))

  • B.添加项目:

    Menu1.Items.Add(new MenuItem("新闻"))

    或者使用它来指定所需的
    新添加的项目的属性:

    MenuItem item = new MenuItem()

    item.NavigateUrl =""

    item.Text = "子测试"

    Menu1.Items.Add(mnuTestChild)

I wish you was more specific but I know that the notification for comments kinda sucks here. so if you could tell me exactly

  1. what is the information you'll Add/Remove items based on ?
  2. how are they're stored in the database (which means are they are stored in one of the membership tables ? in the profile's table ? or a custom table where the user is referenced in ?
  3. what's the technique you imagine in your mind in plain English ?

I'm not sure though how to access
specific menu items from the menu
control and remove them at runtime?

  • A. Remove items:

    Menu1.Items.Remove(Menu1.FindItem("Jobs"))

  • B. Add Items:

    Menu1.Items.Add(new MenuItem("News"))

    OR use this to specify the required
    properties for the new added item:

    MenuItem item = new MenuItem()

    item.NavigateUrl =""

    item.Text = "Child Test"

    Menu1.Items.Add(mnuTestChild)

偏爱你一生 2024-11-09 22:51:21
protected void Page_Load(object sender, EventArgs e)
{
    con.ConnectionString = ConfigurationManager.ConnectionStrings["MyConString"].ConnectionString;
    string selectCmd = "Select * from Pages where Status='"+1+"'";
    SqlDataAdapter dap = new SqlDataAdapter(selectCmd,con);
    DataSet ds = new DataSet();
    dap.Fill(ds);
    if (!Page.IsPostBack)
    {
        int x = 0;
        string parent_id = "";
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            parent_id = dr[0].ToString();
            Menu1.Items.Add(new MenuItem(dr["Title"].ToString(), dr["URL"].ToString(), dr["ID"].ToString()));
        }
        x++;
    }

}
protected void Page_Load(object sender, EventArgs e)
{
    con.ConnectionString = ConfigurationManager.ConnectionStrings["MyConString"].ConnectionString;
    string selectCmd = "Select * from Pages where Status='"+1+"'";
    SqlDataAdapter dap = new SqlDataAdapter(selectCmd,con);
    DataSet ds = new DataSet();
    dap.Fill(ds);
    if (!Page.IsPostBack)
    {
        int x = 0;
        string parent_id = "";
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            parent_id = dr[0].ToString();
            Menu1.Items.Add(new MenuItem(dr["Title"].ToString(), dr["URL"].ToString(), dr["ID"].ToString()));
        }
        x++;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文