如何访问Site.Master页面中的控件?

时间:2016-11-24 02:52:36

标签: asp.net master-pages

我有一个Site.Master页面和文件夹,包含个别"项目"每个都有自己的主页面,以及详细页面:

   {
  "type": "message",
  "timestamp": "2016-11-24T02:30:44.9047645Z",
  "from": {
    "id": "56800324",
    "name": "Bot1"
  },
  "conversation": {
    "id": "8a684db8",
    "name": "Conv1"
  },
  "recipient": {
    "id": "2c1c7fa3",
    "name": "User1"
  },
  "text": "",
  "channelData": {},
  "replyToId": "3f223b935086422da7ed36786ab05cb9"
}

我想在~/Site.Master myProject/Project1.Master myProject/Project1.aspx myProject/Project1.cs 中使用FindControl()来查找Project1.cs中的标签。

我已尝试Site.Master,但它返回null

我已尝试过this.Master.MasterPage.Master,但提及this.Master

我已尝试Project1.Master,但也会返回null。

有什么想法吗?

由于

更新

Page.Parent.Master页面有此指令......

Site.Master

<%@ Master Language="C#" CodeFile="Site.master.cs" Inherits="Site" %> 文件包含这些......

Project1.master

Project1.master的标题......

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="TradeEduTeams.master.cs" Inherits="TET_system" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

更新2

这是我的Site.Master页面全部...

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Trade Education Teams System (TETs)</title>
    <asp:ContentPlaceHolder id="ContentPlaceHolderHead" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
...
...

更新3

我想我知道发生了什么。 <%@ Master Language="C#" CodeFile="Site.master.cs" Inherits="Site" %> <html> <head id="Head1" runat="server"> <title>Mitec</title> </head> <body > <form id="form1" runat="server"> <table width="100%"> <tr valign="top" align="center" > <td align="left"> <asp:Image ID="Image2" runat="server" ImageUrl="~/images/Tec-NQ-RGB-218x90.jpg" /> </td> <td align="center"> <table> <tr> <td align="center"> <asp:Image ID="Image3" runat="server" ImageUrl="~/images/mitec.jpg" /> </td> </tr> <tr> <td align="center"> <asp:LoginName Font-Names="Times New Roman" Font-Size="1.2em" ID="LoginName1" FormatString="login: {0}" CssClass="loginname" runat="server" /> </td> </tr> </table> </td> <td align="right"> <asp:Image ID="Image1" runat="server" ImageUrl="~/images/Tec-NQ-RGB-218x90.jpg" /> </td> </tr> <tr><td colspan="3">&nbsp;</td></tr> <tr> <td colspan="3"> <table> <tr> <td style="width:20%" valign="top"> <asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1" ImageSet="Arrows" ExpandDepth="1" OnTreeNodeDataBound="TreeNode_DataBound" OnDataBound="Tree_DataBound" OnTreeNodeExpanded="TreeNode_Expanded" OnTreeNodeCollapsed="TreeNode_Collapsed" LineImagesFolder="~/TreeLineImages" ShowLines="True"> <ParentNodeStyle Font-Bold="False" /> <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" /> <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px" VerticalPadding="0px" /> <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" /> </asp:TreeView> <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" /> <div class="menu"> </div> </td> <td align="center" style="width:60%" colspan="2" valign="top"> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </td> </tr> </table> </td> </tr> </table> </form> </body> </html> 文件可以包含HTML标记,但不是嵌套的主文件!嵌套母版页必须仅引用内容占位符,以便它不会与&#34;更高版本&#34; site.master文件中的标记。是

2 个答案:

答案 0 :(得分:1)

嗨,请查看下面的示例,我创建了一个BasicView母版页面,其中包含主视图,我已经包含了一个嵌套母版页面,其中包含一些额外的控件..正如您所看到的,BasicView.master中的ContentPlaceHolder1将被替换使用ExtraNested.master中ContentPlaceHolder1内的所有html代码 这就是你如何引用和连接两个母版页的方式..

BasicVew.master:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="BasicView.master.cs" Inherits="WebApplication4.BasicView" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<div>
    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

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

ExtraNested.master

<%@ Master Language="C#" MasterPageFile="~/BasicView.Master" AutoEventWireup="true" CodeBehind="ExtraNested.master.cs" Inherits="WebApplication4.ExtraNested" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" />

所以现在在Button1.click事件的ExtraNested.master里面,您可以尝试使用您的代码找到BasicView.master的控件文本框。

注意:BasicView包含大部分HTML代码,ExtraNested使用了BasicView html,并添加了一些额外的HTML。

答案 1 :(得分:0)

听起来好像您正在寻找MasterType指令。

https://msdn.microsoft.com/en-us/library/c8y19k6h.aspx

基本上,您将MasterType指令放在子母版主页上,并使用它来引用Site.master。