我想從子頁面訪問母版頁代碼文件中的公共類,但無法使其工作。我嘗試使用與訪問公共int相同的方法,如下所示,但子頁面不能識別任何類項目。asp.net - 從子頁面訪問母版頁中的公共類
MasterPage.cs
private int _Section;
public int Section
{
get{return _Section;}
set{_Section = value;}
}
public class HeaderPanel
{
private bool _Visible = true;
public bool Visible
{
get { return _Visible; }
set { _Visible = value; }
}
private string _Theme;
public string Theme
{
get { return _Theme; }
set { _Theme = value; }
}
public HeaderPanel(bool Visible, string Theme)
{
this.Visible = Visible;
this.Theme = Theme;
}
}
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
Master.Section = 1; // This works
Master.HeaderPanel.Visible = true; // This doesn't work
Master.HeaderPanel.Theme = "Dark"; // This doesn't work
}
該錯誤消息我得到的是:
'HeaderPanel':無法通過表達式引用類型
看起來你試圖訪問'HeaderPanel'上的屬性,就好像它們是'static'一樣。你忘了讓它們變成「靜態」嗎?或者忘記在'MasterPage'中創建一個類的實例? – David