从母版页中选择一个菜单项

发布于 2024-11-17 00:04:32 字数 1028 浏览 3 评论 0原文

我目前有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑忘罢 2024-11-24 00:04:32

尝试将代码移至 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

牵你的手,一向走下去 2024-11-24 00:04:32

我通过在母版页中输入以下代码解决了这个问题:

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;
                    }
                }
            }
        }
    }

然后我将以下代码添加到“Translator.aspx”页面:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Session["Translator"] = "true";
        }
    }

我认为这不是理想的解决方案,但它对我有用。

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文