C# 如何从嵌套母版页中的内容页访问主母版页中的控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10062217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:18:32  来源:igfitidea点击:

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

c#asp.netc#-4.0master-pages

提问by Arian

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

我有 2 个嵌套的母版页。这是主要的母版页代码,例如:

<%@ 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 Button1get text of main master page.

我想点击Button1获取主母版页的文本。

How I can do this?

我怎么能做到这一点?

采纳答案by walther

In PageMasterPageNested.aspx:

在 PageMasterPageNested.aspx 中:

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

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

应该管用。试一试。希望能帮助到你。

回答by Guven Sezgin Kurt

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");