内容页面上的母版页控件

时间:2014-08-20 10:15:40

标签: c# master-pages content-pages

我在母版页上有以下代码:

<u1>
...............
............
.............
</u1>
<ul>
    <li id="link1" runat="server"><a href="mytestfile.aspx">Test Files</a></li>
    <li id="link2" runat="server"><a href="mylistitemtest.aspx">List Item Test</a></li>
    <li id="link3" runat="server"><a href="Mytest2.aspx">Some Test</a></li>    
</ul> 

我正在尝试访问内容页面上的link1:

var pageHandler = HttpContext.Current.CurrentHandler;
         if (pageHandler is System.Web.UI.Page)
         {
             ((System.Web.UI.Page)pageHandler).Master.FindControl("nav_link1").Visible = false;
         }

但我得到的错误是: &#34;对象引用未设置为对象的实例&#34;

2 个答案:

答案 0 :(得分:1)

您的母版页上没有名为nav_link1的控件link1

答案 1 :(得分:0)

How to: Reference ASP.NET Master Page Content

在ContentPage上设置@MasterType指令

<%@ MasterType virtualpath="~/Masters/Master1.master" %>

然后使用FindControl在MasterPage上找到你想要的东西

ContentPlaceHolder mpContentPlaceHolder;
TextBox mpTextBox;
mpContentPlaceHolder = 
  (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if(mpContentPlaceHolder != null)
{
    mpTextBox = 
        (TextBox) mpContentPlaceHolder.FindControl("TextBox1");
    if(mpTextBox != null)
    {
        mpTextBox.Text = "TextBox found!";
    }
}