ASP.net get content page to change master page control

匆匆过客 提交于 2019-12-23 02:36:07

问题


Master page:

<form runat="server">
<Scirra:MainMenu runat="server" ID="MainMenu" TopTabSelected="home" SubTabSelected="link2" />
<asp:ContentPlaceHolder id="MainContent" runat="server">
snip

Content page:

Master.MainMenu.TopTabSelected = "forum";

I know I'm probably doing this wrong, but is this possible? I want to change a parameter of that control. It says 'inaccessible due to protection level'.


回答1:


You should provide a public property f.e MenuTabSelected in your MasterPage that Gets/Sets this property of your Menu.

public string MenuTabSelected {
    get { return MainMenu.TopTabSelected; }
    set { MainMenu.TopTabSelected = value; }
}

Then you can access it in this way:

((YourMasterPage)Master).MenuTabSelected = "forum";

where YourMasterPage is the type of your MasterPage.

The compiler error is thrown because you want to access a private or protected control from outside of your MasterPage-Class. This would only be allowed if it would be public, what is not recommended. You have more control if you do it the way i suggested :)




回答2:


find menu items in content page and change its value

protected void Page_Load(object sender, EventArgs e)
{


Menu mainMenu = (Menu)Page.Master.FindControl("NavigationMenu");

MenuItem menuMaterials = mainMenu.FindItem("Materials");

    if (menuMaterials.Value == "Materials")
    {
         menuMaterials.Value = "NO materials";
        menuMaterials.Text = "No materials";
    }

}   


来源:https://stackoverflow.com/questions/5207500/asp-net-get-content-page-to-change-master-page-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!