[英]Select a menu item from Master Page
I currently have an asp Menu Control which loads a SiteMapDataSource in my Master Page. 我目前有一个ASP菜单控件,可在我的母版页中加载SiteMapDataSource。 One of the site map nodes is "Tools" which opens a general "Tools.aspx" content page.
站点地图节点之一是“工具”,它将打开常规的“ 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>
The "Tools.aspx" page contains an image button that takes the user to another content page "Translator.aspx". “ Tools.aspx”页面包含一个图像按钮,可将用户带到另一个内容页面“ 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?
我的问题是,如何从“ Translator.aspx”页面的母版页中选择“工具”菜单项?
I have tried the following method within the "Translator.aspx" page load: 我已经在“ 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;
}
}
}
}
This does not work and it appears that 0 menu items are returned. 这不起作用,似乎返回了0个菜单项。
Would really appreciate it if someone could shed some light on this issue. 如果有人可以阐明这个问题,我将不胜感激。 Thanks.
谢谢。
尝试将代码移至pre_render或生命周期中的某个后期,以确保在加载后未加载菜单
I solved this by entering the following code in the Master Page: 我通过在母版页中输入以下代码来解决此问题:
protected void mnuMaster_MenuItemDataBound(object sender, MenuEventArgs e)
{
if (Session["Translator"] != null)
{
if (mnuMaster.Items.Count > 0)
{
foreach (MenuItem mi in mnuMaster.Items)
{
if (mi.Text == "Tools")
{
mi.Selected = true;
Session["Translator"] = null;
}
}
}
}
}
I then added the following to the "Translator.aspx" page: 然后,将以下内容添加到“ Translator.aspx”页面:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["Translator"] = "true";
}
}
I don't think this is the ideal solution but it worked for me. 我认为这不是理想的解决方案,但对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.