代码之家  ›  专栏  ›  技术社区  ›  BillB

在ASP.NET菜单控件中设置item.selected

  •  2
  • BillB  · 技术社区  · 16 年前

    这里是ASP.NET新手。在页面上,我想将相应的菜单项设置为选中。我的方法是: 在home.aspx.cs上:

    Menu menu = (Menu)Master.FindControl("Menu1");
    
    if (menu.Items.Count > 0)
    {
        menu.FindItem("Home").Selected = true;
    }
    

    麻烦是, menu.item.count == 0 . 我的菜单绑定到一个站点地图,如果这很重要的话。

    3 回复  |  直到 12 年前
        1
  •  4
  •   Leniel Maccaferri    16 年前

    我认为您必须在menuitemdatabound事件上设置所选项目(调整代码):

    protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs e)
    {
        if (SiteMap.CurrentNode != null)
        {
            if (e.Item.Text == SiteMap.CurrentNode.Title)
            {
                e.Item.Selected = true;
            }
        }
    }
    

    更多内容,显示如何在具有网站图数据源的菜单中处理链接…

    若要在新窗口中打开从web.sitemap生成的菜单链接…

    在ASP.NET页中,添加OnMenuItemDataBound事件:

    <asp:Menu ID="mnuFooter" runat="server"
    DataSourceID="SiteMapDataSource1"
    OnMenuItemDataBound="mnuFooter_MenuItemDataBound">
    </asp:Menu>
    

    在web.sitemap中,添加?URL的字符:

    在代码隐藏中,捕获menuitemdatabound事件:

    protected void mnuFooter_MenuItemDataBound(Object sender, MenuEventArgs e)
    {
        if (e.Item.NavigateUrl.Contains("?"))
        {
            e.Item.Target = "_blank";
        }
    }
    

    web.sitemap中包含的任何URL?将在新窗口中打开。注意,使用任何其他有效的URL字符来代替?如有必要。

    ASP.NET Menu Control Overview

        2
  •  0
  •   BillB    16 年前

    好吧,这就是我的结局。(谢谢,莱尼尔,你指点了我的方向。) 以下代码位于包含菜单控件的主文件后面的代码中。 代码中的注释显示了我的惊讶,这个控件不允许一次选择多个项。所以,在一个多级菜单中,我不能同时显示当前节点和它的父节点。(除非我错过了什么。)

    // This is where we set the current node's ROOT menu item's selected property to
        // true.  The current node won't show as selected, but it's parent will be.
        // As explained elsewhere, this was a UI decision based on the fact that the menu 
        // can only have one menu item selected at a time.
        // If the menu wasn't dataBound, this code would be in Page_Load.
        protected void SideMenu_DataBound1(object sender, EventArgs e)
        {           
            Menu menu = (Menu)this.FindControl("SideMenu");    // Get Menu Object
    
            string parent = null;
    
            // First check that there's a current node - there wont be one if the user
            // accesses this page from a bookmark.
            if (SiteMap.CurrentNode != null)
            {
                // Check if the current place in the SiteMap is in the first level of the menu
                // or a child menu item. 
                if (SiteMap.CurrentNode.ParentNode.ParentNode != null)
                    parent = SiteMap.CurrentNode.ParentNode.Description;  // It's a child - has a parent
    
                if (parent == null)  // a parent node
                {
                    MenuItem item = menu.FindItem(SiteMap.CurrentNode.Description);
                    item.Selected = true;
                }
                else   // a child menu item
                {
                    // Get it's parent node and set it's selected property to true.
                    MenuItem item = menu.FindItem(parent);
                    item.Selected = true;
                    // Following comments left in to show how to get at
                    // a menu item that's not in the first level.  The trick is the
                    // "/" separator, which is the pathSeparator property of the menu
                    // control.
    
                    //  The menu control
                    // only allows one item to be selected at a time.  This is true, even though
                    // the MenuItem class has a selected property, which implies each item 
                    // can have a selected property but it's not the case.
                    //path = parent + "/" + current;
                    //MenuItem item2 = menu.FindItem(path);
                    //item2.Selected = true;
                }
            }
        }
    
        3
  •  0
  •   Scott Marcus    14 年前

    如果web.sitemap文件包含外部链接(指向其他域的链接),则这些链接将需要以http://或https://开头,但本地文件引用不会-它们只是来自加载文档的当前位置的相对引用。

    因此,当您需要的字符已经存在时(HTTP或HTTPS),不需要用额外的字符填充URL。

    protected void mnuFooter_MenuItemDataBound(Object sender, MenuEventArgs e) 
    { 
        string theURL = e.Item.NavigateUrl;
        if (theURL.Contains("http://") || theURL.Contains("https://")) 
        { 
            e.Item.Target = "_blank"; 
        } 
    } 
    

    如果要在新窗口中打开本地文件,只需将URL从本地引用更改为完整的URL(加载该文件需要稍长时间,因为必须解析该URL)。