这里是 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
右键单击 Silverlight 4 应用程序中的列表框Right-click on a Listbox in a Silverlight 4 app(右键单击 Silverlight 4 应用程序中的列表框)
WPF c# webbrowser 在顶部菜单上滚动WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在顶部菜单上滚动)
C# 控制台应用程序 - 如何制作交互式菜单?C# Console app - How do I make an interactive menu?(C# 控制台应用程序 - 如何制作交互式菜单?)
如何向 System.Windows.Forms.MenuItem 添加图标?How to add an icon to System.Windows.Forms.MenuItem?(如何向 System.Windows.Forms.MenuItem 添加图标?)
如何避免在 .NET Windows Forms 中创建重复的表单?How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中创建重复的表单?)
使用 ASP.NET、JQuery 和 Suckerfish 构建数据库驱动的Building a database driven menu with ASP.NET, JQuery and Suckerfish(使用 ASP.NET、JQuery 和 Suckerfish 构建数据库驱动的菜单)