ASP.NET 嵌套母版页中找不到控件问题(未已用到对象错误)

来源:互联网 发布:linux cat sed 编辑:程序博客网 时间:2024/06/05 09:47

故障现象:

三层嵌套母版页,用findcontrol方法只能找到第一层母版页的控件,找不到二层和三层的,提示未引用到对象实例错误。

原因分析:

Asp.net的确找不到嵌套母版页的控件,只能找到最顶层母版页的控件,即Master.Master.Master.FindControl才有效。

解决方法:

找到最顶层的容器ContentPlaceHolder

然后通过顶层容器找(findcontrol)第二层容器,在通过第二层容器找第三层容器,再从第二、三层容器中找控件。

实操示例:



顶层母版页:

<%@MasterLanguage="C#"AutoEventWireup="true"CodeFile="MasterPage1.master.cs"Inherits="MasterPage1"%>

 

<!DOCTYPEhtmlPUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

    <title></title>

    <asp:ContentPlaceHolderid="CPH_master1_head"runat="server">

    </asp:ContentPlaceHolder>

</head>

<body>

    <formid="form1"runat="server">

    <div>

 

        <asp:LabelID="Label_master1"runat="server"Text="Label"></asp:Label>

        <hr/>

        <asp:ContentPlaceHolderid="CPH_master1_content"runat="server">

        </asp:ContentPlaceHolder>

    </div>

    </form>

</body>

</html>

第二层母版页:

<%@MasterLanguage="C#"MasterPageFile="~/MasterPage1.master"AutoEventWireup="true"CodeFile="MasterPage2.master.cs"Inherits="MasterPage2"%>

 

<asp:ContentID="Content21"ContentPlaceHolderID="CPH_master1_head"Runat="Server">

    <asp:ContentPlaceHolderID="CPH_master2_head"runat="server">

   

  </asp:ContentPlaceHolder>

</asp:Content>

<asp:ContentID="Content22"ContentPlaceHolderID="CPH_master1_content"Runat="Server">

    <asp:LabelID="Label_master2"runat="server"Text="Label"></asp:Label>

    <hr/>

<asp:ContentPlaceHolderID="CPH_master2_content"runat="server">

   

  </asp:ContentPlaceHolder>

</asp:Content>

第三层母版页:

<%@MasterLanguage="C#"MasterPageFile="~/MasterPage2.master"AutoEventWireup="true"CodeFile="MasterPage3.master.cs"Inherits="MasterPage3"%>

 

<asp:ContentID="Content31"ContentPlaceHolderID="CPH_master2_head"Runat="Server">

    <asp:ContentPlaceHolderID="CPH_master3_head"runat="server">

   

  </asp:ContentPlaceHolder>

</asp:Content>

<asp:ContentID="Content32"ContentPlaceHolderID="CPH_master2_content"Runat="Server">

    <asp:LabelID="Label_master3"runat="server"Text="Label"></asp:Label>

    <hr/>

<asp:ContentPlaceHolderID="CPH_master3_content"runat="server">

   

  </asp:ContentPlaceHolder>

</asp:Content>

内容页:

页面文件:

<%@PageTitle=""Language="C#"MasterPageFile="~/MasterPage3.master"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>

<%@MasterTypeVirtualPath="~/MasterPage3.master"%>

<asp:ContentID="Content1"ContentPlaceHolderID="CPH_master3_head"Runat="Server">

</asp:Content>

<asp:ContentID="Content2"ContentPlaceHolderID="CPH_master3_content"Runat="Server">

    <asp:LabelID="Label_content"runat="server"Text="Label"></asp:Label>

</asp:Content>

代码文件:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partialclass_Default :System.Web.UI.Page

{

    protected void Page_Load(objectsender, EventArgs e)

    {

        ContentPlaceHoldercph2 = Master.Master.Master.FindControl("CPH_master1_content")asContentPlaceHolder;

        if(cph2 != null)

        {

            ContentPlaceHoldercph3 = cph2.FindControl("CPH_master2_content")asContentPlaceHolder;

            if(cph3 != null)

            {

                Labellb3 = cph3.FindControl("Label_master3")asLabel;

                if(lb3 != null) lb3.Text = "This is a Label of master3 Page.";

            }

            Labellb2 = cph2.FindControl("Label_master2")asLabel;

            if(lb2 != null) lb2.Text = "This is a Label of master2 Page.";

        }

        Labellb1 = Master.Master.Master.FindControl("Label_master1")asLabel;

        if (lb1!= null) lb1.Text = "Thisis a Label of master1 Page.";

        Label_content.Text = "This is a Label of content Page.";

    }

}

参考网帖:

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 = "MyTitle";

                }

           }

       }

    }

 

附:

主模版页:

<%@ 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:contentid="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" %>

<%@ MasterTypeVirtualPath="~/Master/Combines.master" %>

 

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

</asp:Content>

 

分类: VS.Net2005

0 0
原创粉丝点击