【问题标题】:Set item.selected in ASP.NET Menu Control在 ASP.NET 菜单控件中设置 item.selected
【发布时间】:2011-02-15 17:40:22
【问题描述】:

这里是 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。 我的菜单绑定到站点地图,如果这很重要的话。

【问题讨论】:

  • 你用哪种方法调用上面显示的代码?也许你在菜单填满之前调用它。
  • 代码在Home.aspx的page_load中。可能是因为菜单绑定到站点地图数据源,因此尚未填充。我想知道这一点。人们必须有这个工作,但我什么也找不到。我将尝试在没有数据源的情况下设置项目 - 看看这会如何影响事情。
  • 是的,我在设计器中设置了项目,它们在 page_load 中。所以我想如果菜单是绑定的,你不能这样做?也许站点地图对象中的某些内容...(这些控件不仅仅是学习javascript。)
  • 灯泡。就像 Leniel 说的 - Master 中的 DataBound 事件可能会这样做。谢谢。

标签: asp.net menu controls


【解决方案1】:

我认为您必须在 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 字符代替 ?如有必要。

ASP.NET Menu Control Overview

【讨论】:

  • 我有一个后续问题:如果我不使用站点地图,但使用普通的 Asp.net:menu - 我如何为所选问题设置菜单项的样式?我希望用户单击某个菜单项后保存样式(以创建伪选项卡控件)
【解决方案2】:

好的,这就是我最终的结果。 (谢谢,Leniel,为我指明了正确的方向。) 以下代码位于包含菜单控件的主文件的代码后面。 代码中的注释让我感到惊讶的是,这个控件不允许一次选择多个项目。因此,在多级菜单中,我无法同时显示当前节点及其父节点。 (除非我遗漏了什么。)

// 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】:

    如果您的 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 必须解析)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-19
      • 1970-01-01
      相关资源
      最近更新 更多