【问题标题】:How to access controls in Main Master page from content page in Nested Master Page如何从嵌套母版页中的内容页访问主母版页中的控件
【发布时间】:2017-10-26 17:21:53
【问题描述】:

我有 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>

和嵌套母版页:

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

我基于这个嵌套母版页创建了一个页面:

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

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

我该怎么做?

【问题讨论】:

    标签: c# asp.net c#-4.0 master-pages


    【解决方案1】:

    在 PageMasterPageNested.aspx 中:

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

    应该可以。试试看。希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      这在任何情况下都有效。特别是如果您不知道或不关心您嵌套了多少母版页。希望对你有帮助:)

      MasterPage tmp = this.Master;
      while (tmp.Master != null)
      {
          tmp = tmp.Master;
      }
      
      var control = tmp.FindControl("form1");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-05
        • 2017-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-14
        • 2014-09-20
        相关资源
        最近更新 更多