How to access controls in Main Master page from content page in Nested Master Page

旧时模样 提交于 2019-12-07 06:41:31

问题


I have 2 master pages that are nested.this is main master page code for example:

<%@ 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>

and the nested master page:

<%@ 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>

and I create a page based on this nested master page:

<%@ 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>

I want in click of Button1 get text of main master page.

How I can do this?


回答1:


In PageMasterPageNested.aspx:

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

Should work. Give it a try. Hope it helps.




回答2:


This works in any case. especially if you dont know or care how many master pages you nested. Hope it helps :)

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

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


来源:https://stackoverflow.com/questions/10062217/how-to-access-controls-in-main-master-page-from-content-page-in-nested-master-pa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!