Asp.net : 訪問嵌套模版頁中的控件

一。如果要訪問的控件位於母版頁的 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")

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章