访问C#中类文件中的母版页

use*_*862 4 c# asp.net master-pages class

我在Master Page中有以下内容:

<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> 
Run Code Online (Sandbox Code Playgroud)

我有一个被调用的类data_class.cs,我在这个类中创建了以下方法来禁用母版页上的控件:

public static void disablecontrol()
{
    Master.FindControl("link1").Visible = false;
    Master.FindControl("Link3").Visible = false;
}
Run Code Online (Sandbox Code Playgroud)

我在使用"主"字时出现以下错误.

an object reference is Required for non-staticfield, method, property 'System.Web.UI.MasterPage.master.get'

Roy*_*mir 13

试试这个 :

var pageHandler = HttpContext.Current.CurrentHandler;
if (pageHandler  is  System.Web.UI.Page)
{
  ((System.Web.UI.Page)pageHandler).Master.FindControl("...").Visible=false;
}
Run Code Online (Sandbox Code Playgroud)