MasterPage
<asp:Button ID="Button1" runat="server" Text="頂層" />
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
MasterPage2是MasterPage的子母版頁
<asp:Button ID="Button1" runat="server" Text="第二層" />
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
test.aspx使用MasterPage2
var masterpage = Master as MasterPage; //第二層MasterPage2
var masterpage2 = masterpage.Master as MasterPage;//頂層MasterPage
var content = masterpage2.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
if (content != null)
{
//第二層(即上一層)需要通過頂層來查找ContentPlaceHolder1,然后找控件
Button bt2 = content.FindControl("Button2") as Button;
if (bt2 != null)
{
string test = bt2.Text;
}
}
//頂層可以直接找到
Button bt = masterpage2.FindControl("Button1") as Button;
if (bt != null)
{
string test = bt.Text;
}