【问题标题】:access master page control from content page in c#从 C# 中的内容页面访问母版页控件
【发布时间】:2014-09-20 13:10:51
【问题描述】:

我的菜单如下:

<ul class="menu" id="menu" runat ="server"> 
     <li id="menulink">
         <a href="#" class="menulink">Mant</a>
             <ul> 
                 <li id="mant"><a href="Mant.aspx">Table</a></li> 
            </ul> 
     </li> 
</ul>

在主页加载时,我想根据访问权限禁用 mant 子菜单。 有没有办法在页面加载时实现这一点。

【问题讨论】:

  • 我尝试使用:Menu MasterPageMenu = (Menu)this.Master.FindControl("menu").FindControl("mant") as Menu; MasterPageMenu.Visible = false;但它给出了空引用错误

标签: asp.net master-pages submenu


【解决方案1】:

在 Page_Load 事件处理程序上需要这样的东西:

Menu MasterPageMenu = (Menu)this.Master.FindControl("menu");

foreach (MenuItem mi in MasterPageMenu.Items[0].ChildItems)
{
    //Do your logic.
}

【讨论】:

  • 我尝试使用:Menu MasterPageMenu = (Menu)this.Master.FindControl("menu").FindControl("mant") as Menu; MasterPageMenu.Visible = false;但它给出了空引用错误
【解决方案2】:

在您的母版页代码隐藏类中提供一个公共方法 ShowMant(bool show)。将内容页面的 Master 属性转换为您的主服务器的实际类型并调用该方法。您需要使 li runat=server 以该方法在服务器端访问它。

类似的东西(我不知道“根据访问权限禁用 mant 子菜单”是什么意思):

public void ShowMant(bool show)
{
    if(show)
        mant.Attributes["class"] = "showMantClass";
    else
        mant.Attributes["class"] = "hideMantClass";
}

您以这种方式使用它(在内容页面中):

var myMaster = this.Master as MyMasterType;
if(myMaster != null)
    myMaster.ShowMant(false);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多