Fernando68 Fernando68 - 14 days ago 8
ASP.NET (C#) Question

How to access controls in Site.Master page?

I have a Site.Master page and folders with individual "projects" that each have their own Master page, as well as the detail page:

~/Site.Master
myProject/Project1.Master
myProject/Project1.aspx
myProject/Project1.cs


I want to use
FindControl()
in
Project1.cs
to find a label that's in
Site.Master
.

I've tried
this.Master.Master
, but it returns null

I've tried
Page.Master
or
this.Master
but that refers to
Project1.Master


I've tried
Page.Parent.Master
but that also returns null.

Any ideas?

Thanks

UPDATE

The
Site.Master
page has this directive...

<%@ Master Language="C#" CodeFile="Site.master.cs" Inherits="Site" %>


The
Project1.master
file has these ...

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


The header of Project1.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>
...
...


UPDATE 2

Here is my Site.Master page in full...

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


UPDATE 3

I think I know what's going on. The
Site.master
file can have HTML tags, but not the nested Master file! The nested master page has to reference Content placeholders only, so that it does not clash with the "higher up" tags in the site.master file. Yes?

Answer

Hi please check the example below, i have created a BasicView Master page which includes the main view and i have included a Nested Master page which include some extra controls.. As you can see the ContentPlaceHolder1 in BasicView.master will be replaced with all html code that is inside ContentPlaceHolder1 in ExtraNested.master This is how you reference and connect 2 master pages with each others..

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" />

So now inside ExtraNested.master on Button1.click event you may try to find control textbox of BasicView.master using your code.

NB: BasicView have most of HTML code and ExtraNested used BasicView html with addition to some extra html that it includes.