遇到一些麻烦引用文字在用户控件包含在一个母版页 [英] Having some trouble referencing a Literal in a user control that is included in a Master Page
问题描述
遇到了一些麻烦引用文字中包含在一个母版页的用户控件。我用下面的测试页面(路径可能包含不必要的...):
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 (相关内容)
<%@ 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 (相关内容)
<%@ 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 (相关内容)
protected void Page_Load(object sender, EventArgs e)
{
Literal mpLiteral = (Literal)Master.FindControl("contentTitle");
if (mpLiteral != null)
{
mpLiteral.Text = "Customer Service Home";
}
}
不知道我做错了,所以希望有人能指出我的方式错误(S)...
Not sure what I'm doing wrong, so hopefully someone can point out the error(s) of my ways...
推荐答案
由于文字是一个用户控件,它是母版页里面,硕士的FindControl
将不能够找到它。
Since the Literal is in a UserControl that is inside the Master Page, Master's FindControl
won't be able to find it.
如果你想在UC的文字提供给大师,创造了UC的code-后面的属性:
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; } }
然后,在你的母版页,您可以访问 header.MyLiteral
来设置/获取值。
现在,如果要使其子页访问,你会然后再公开为属性,但这次在主人的code-背后:
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; } }
最后,在子页面,你需要投 this.Master
你的母版页的类型(我认为这是 master_pages_partner
根据你的MP标记):
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";
}
这篇关于遇到一些麻烦引用文字在用户控件包含在一个母版页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!