从母版页中选择一个菜单项
我目前有一个 asp 菜单控件,它在我的母版页中加载 SiteMapDataSource。站点地图节点之一是“工具”,它打开一般的“Tools.aspx”内容页面。
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="~/Default.aspx" title="Home" description="">
<siteMapNode url="Tools.aspx" title="Tools" description="" />
</siteMapNode>
</siteMap>
“Tools.aspx”页面包含一个图像按钮,可将用户带到另一个内容页面“Translator.aspx”。导航到此页面时,不再选择“工具”菜单项。我的问题是,如何从“Translator.aspx”页面中的母版页中选择“工具”菜单项?
我在“Translator.aspx”页面加载中尝试了以下方法:
protected void Page_Load(object sender, EventArgs e)
{
//check if logged in
if (!Page.IsPostBack)
{
Menu mp_Menu = (Menu)Page.Master.FindControl("mnuMaster");
foreach (MenuItem mi in mp_Menu.Items)
{
if (mi.Text == "Tools")
{
mi.Selected = true;
}
}
}
}
这不起作用,并且似乎返回了 0 个菜单项。
如果有人能够阐明这个问题,我将非常感激。
I currently have an asp Menu Control which loads a SiteMapDataSource in my Master Page. One of the site map nodes is "Tools" which opens a general "Tools.aspx" content page.
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="~/Default.aspx" title="Home" description="">
<siteMapNode url="Tools.aspx" title="Tools" description="" />
</siteMapNode>
</siteMap>
The "Tools.aspx" page contains an image button that takes the user to another content page "Translator.aspx". When navigating to this page the "Tools" menu item is no longer selected. My question is, how can I select the "Tools" menu item from the master page, within the "Translator.aspx" page?
I have tried the following method within the "Translator.aspx" page load:
protected void Page_Load(object sender, EventArgs e)
{
//check if logged in
if (!Page.IsPostBack)
{
Menu mp_Menu = (Menu)Page.Master.FindControl("mnuMaster");
foreach (MenuItem mi in mp_Menu.Items)
{
if (mi.Text == "Tools")
{
mi.Selected = true;
}
}
}
}
This does not work and it appears that 0 menu items are returned.
Would really appreciate it if someone could shed some light on this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将代码移至 pre_render 或生命周期后期的某些位置,以确保加载后不会加载菜单
try moving the code to pre_render or something later in the lifecycle just to make sure that the menus aren't being loaded after load
我通过在母版页中输入以下代码解决了这个问题:
然后我将以下代码添加到“Translator.aspx”页面:
我认为这不是理想的解决方案,但它对我有用。
I solved this by entering the following code in the Master Page:
I then added the following to the "Translator.aspx" page:
I don't think this is the ideal solution but it worked for me.