2012-04-08 20 views
5

Ho 2 pagine master che sono nested.this è principale codice di pagina master per esempio:Come accedere controlli nella pagina Main Master da pagina di contenuto in nidificate pagina master

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPageMaster.master.cs" Inherits="MasterPageMaster" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <asp:ContentPlaceHolder id="head" runat="server"> 
    </asp:ContentPlaceHolder> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
    <asp:TextBox ID="txtMasterPageMaster" ClientIDMode="Static" runat="server"></asp:TextBox> 
    <div style="background-color:Aqua;height:40px;"> 
    Some Text 
    </div> 
    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"> 

    </asp:ContentPlaceHolder> 
</div> 
</form> 

e la pagina master nidificate:

<%@ Master Language="C#" MasterPageFile="~/MasterPageMaster.master" AutoEventWireup="true" 
CodeFile="MasterPageNested.master.cs" Inherits="MasterPageNested" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 
    <asp:Panel runat="server" ID="panelMain" BackColor="lightyellow"> 
    <h2> 
     Child master</h2> 
    <asp:Panel runat="server" ID="panel1" BackColor="lightblue"> 
     <p> 
      This is child master content.</p> 
     <asp:ContentPlaceHolder ID="ChildContent1" runat="server" /> 
    </asp:Panel> 
    <asp:Panel runat="server" ID="panel2" BackColor="pink"> 
     <p> 
      This is child master content.</p> 
     <asp:ContentPlaceHolder ID="ChildContent2" runat="server" /> 
    </asp:Panel> 
    <br /> 
</asp:Panel> 
</asp:Content> 

e creo una pagina basata su questa pagina master nidificate:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPageNested.master" AutoEventWireup="true" CodeFile="PageMasterPageNested.aspx.cs" Inherits="PageMasterPageNested" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="ChildContent1" Runat="Server"> 
</asp:Content> 
    <asp:Content ID="Content2" ContentPlaceHolderID="ChildContent2" Runat="Server"> 
    <asp:Button ID="Button1" runat="server" Text="Button" Height="66px" 
    onclick="Button1_Click" Width="196px" /> 
</asp:Content> 

Desidero fare clic su Button1 per ottenere il testo della pagina principale principale.

Come posso fare questo?

risposta

17

In PageMasterPageNested.aspx:

TextBox txtBox = this.Master.Master.FindControl("txtMasterPageMaster") as TextBox; 

dovrebbe funzionare. Provaci. Spero che sia d'aiuto.

0

Questo funziona in ogni caso. soprattutto se non conosci o ti interessa quante pagine mastro hai annidato. Spero che lo aiuti :)

MasterPage tmp = this.Master; 
while (tmp.Master != null) 
{ 
    tmp = tmp.Master; 
} 

var control = tmp.FindControl("form1"); 
Problemi correlati