在 ASP.NET 菜单控件中设置 item.selected [英] Set item.selected in ASP.NET Menu Control

查看:23
本文介绍了在 ASP.NET 菜单控件中设置 item.selected的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

ASP.NET newbie here. When on a page I'd like to set the corresponding menu item to selected. My approach is this: On Home.aspx.cs:

Menu menu = (Menu)Master.FindControl("Menu1");

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

问题是,menu.item.count == 0.如果重要的话,我的菜单已绑定到站点地图.

Trouble is, menu.item.count == 0 . My menu is bound to a sitemap, if that matters.

推荐答案

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

I think you must set the selected item on MenuItemDataBound event (adapt your code):

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

更多内容展示了如何处理以站点地图作为数据源的菜单中的链接...

More content that shows how to handle links in a menu that has as datasource a sitemap...

要在新窗口中打开从 web.sitemap 构建的菜单链接...

To have a menu link built from web.sitemap open in new window...

在asp.net页面添加OnMenuItemDataBound事件:

In asp.net page add OnMenuItemDataBound event:

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

在 web.sitemap 中,添加一个 ?url 的字符:

In web.sitemap, add a ? character to the url:

在后面的代码中,捕获 MenuItemDataBound 事件:

In code behind, capture the MenuItemDataBound event:

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

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

Any url in the web.sitemap that contains a ? will open in a new window. Note, use any other valid url character in place of the ? if necessary.

ASP.NET 菜单控件概述

这篇关于在 ASP.NET 菜单控件中设置 item.selected的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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