我想在內容頁的主頁中設置一個標籤,而不是使用FindControl。所以,在我宣佈的母版頁中:在主頁中找到控件
public partial class MainMasterPage : System.Web.UI.MasterPage
{
public string UserOfficeLabel
{
get { return lblUserOffice.Text; }
set { lblUserOffice.Text = value; }
}
public string OfficeLocationLabel
{
get { return lblOfficeLocation.Text; }
set { lblOfficeLocation.Text = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
....
}
}
「UserOfficeLabel」和「OfficeLocationLabel」是母版頁上的標籤。然後,在內容頁(的.aspx)我加了下面的指令下的「頁面」指令:
<%@ MasterType VirtualPath="~/Main/MainMasterPage.master" %>
在conent頁面的代碼背後(cs文件),我試圖訪問/設置標籤:
Master.UserOfficeLabel = ...
但UserOfficeLabel不適用於Master(VS智能感知不會將其列爲選項)。當我添加它時,它說「MainMasterPage.UserOfficeLabel無法訪問其保護級別」
爲什麼你不想使用FindControl? –
「我有一顆螺絲,我想把它擰成螺絲刀,但我不想用螺絲刀,請幫忙,不,我不會告訴你爲什麼。」 – asawyer
從OO/Encapsulation的角度來看,使用FindControl是不好的,應儘可能避免。這類似於通過C++中的指針公開私有變量。我希望這回答了你的問題。 – NoBullMan