Reputation: 301
Having some trouble referencing a Literal in a user control that is included in a Master Page. I'm testing using the following pages (paths probably included unnecessarily...):
~/_inc/header.ascx
~/master_pages/partner_header_footer.master
~/credit_check/test_page.aspx
header.ascx (relevant content)
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="header.ascx.cs" Inherits="_inc_header" %>
<link type="text/css" rel="stylesheet" href="/css/header.css" />
<div id="header" class="content-header cf">
<div id="logo"></div><asp:Literal id="contentTitle" runat="server" Text="Customer Service" />
</div>
partner_header_footer.master (relevant content)
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="partner_header_footer.master.cs" Inherits="master_pages_partner" %>
<%@ Register Src="~/_inc/header.ascx" TagPrefix="uc1" TagName="header" %>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>Wireless</title>
</head>
<body>
<form id="form1" runat="server">
<uc1:header runat="server" ID="header" />
<asp:ContentPlaceHolder id="Wireless" runat="server">
</asp:ContentPlaceHolder>
</form>
</body></html>
test_page.cs (relevant content)
protected void Page_Load(object sender, EventArgs e)
{
Literal mpLiteral = (Literal)Master.FindControl("contentTitle");
if (mpLiteral != null)
{
mpLiteral.Text = "Customer Service Home";
}
}
Not sure what I'm doing wrong, so hopefully someone can point out the error(s) of my ways...
Upvotes: 1
Views: 1083
Reputation: 3289
Since the Literal is in a UserControl that is inside the Master Page, Master's FindControl
won't be able to find it.
If you want to make the UC's Literal available to the Master, create a Property in the UC's code-behind:
public string MyLiteral { get{ return contentTitle.Text; } set{ contentTitle.Text = value; } }
Then, in your Master Page, you can access header.MyLiteral
to set/get the value.
Now, if you want to make it accessible to Child Pages, you'd then expose it again as a Property, but this time in the Master's code-behind:
public string HeaderLiteral { get{ return header.MyLiteral; } set{ header.MyLiteral = value; } }
Finally, in the Child Page, you'll need to cast this.Master
to the type of your Master Page (I think it's master_pages_partner
based on your MP markup):
var castedMaster = (master_pages_partner)this.Master;
if(null != castedMaster)
{
castedMaster.HeaderLiteral = "Customer Service Home";
}
Upvotes: 1