您的位置:首页 > 编程语言 > ASP

Asp.net : 访问嵌套模版页中的控件

2007-05-06 10:25 183 查看
一。如果要访问的控件位于母版页的 ContentPlaceHolder 控件内部,必须首先获取对 ContentPlaceHolder 控件的引用,然后调用其 FindControl 方法获取对该控件的引用。

在内容页的Page_Load方法中设置嵌套模版页中label控件的Text属性:
首先获取主模版页的ContentPlaceHolder控件的引用,然后再获取要修改的控件的引用。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ContentPlaceHolder pContent = (ContentPlaceHolder)Page.Master.Master.FindControl("Main");
if (pContent != null)
{
Label pLblTitle = (Label)pContent.FindControl("lbl_title");
if (pLblTitle != null)
{
pLblTitle.Text = "My Title";
}
}
}
}

附:
主模版页:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="Master_MasterPage" %>

<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:contentplaceholder id="Main" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>

嵌套模版页:
<%@ Master Language="C#" MasterPageFile="~/Master/Default.master" AutoEventWireup="true" CodeFile="Combines.master.cs" Inherits="Master_Combines" %>

<asp:content id="Content_combine" contentplaceholderid="Main" runat="server">
<asp:Label ID="lbl_title" runat="server" Text=""></asp:Label>
<asp:contentplaceholder id="Combine" runat="server">
</asp:contentplaceholder>
</asp:content>

内容页:
<%@ Page Language="C#" MasterPageFile="~/Master/Combines.master" AutoEventWireup="true" CodeFile="StationCombine.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
<%@ MasterType VirtualPath="~/Master/Combines.master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Combine" Runat="Server">
</asp:Content>

二。

master-------------------------------------------------------------------------------------------
| | |
| cpm(ContentPlaceHolder) ml(WebUserControl)
---- default.asp |
| myq(LinkButton)
C1(cmp)
|
Phmain(PlaceHolder)

在单击myq中对phmain进行操作visable属性操作
在myq的click中
通过Me.Parent.FindControl("cpm").FindControl("phmain")来得到phmain
Dim ph As PlaceHolder = (Me.Parent.FindControl("cpm").FindControl("phmain"))
ph.Visible = True一样的,可以用Me.master得到master中的对像
三。

如果master里面的模板ID为ContentPlaceHolder2,下面是html控件,ID为li2.

下面为引用方法,这个是设置样式的。

Dim control As System.Web.UI.HtmlControls.HtmlGenericControl
control = Me.Page.Master.FindControl("ContentPlaceHolder2").FindControl("li2")
control.Attributes.Add("class", "current_page_item")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: