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
- ASP.NET 嵌套母版页中找不到控件问题(未已用到对象错误)
- asp.net母版页使用控件时,控件中路径问题
- 嵌套的 ASP.NET 母版页
- C# asp.net 母版页嵌套
- 嵌套的 ASP.NET 母版页
- ASP.NET母版页中对控件ID的处理
- 嵌套母版页中访问顶级母版页中的控件
- ASP.Net控件母版页专题
- ASP.NET 子页找母版页控件
- ASP.NET MVC3.0 中 Razor 母版嵌套
- asp.net动态编辑母版页(与母版页通信,如修改控件属性等)
- asp.net动态编辑母版页(与母版页通信,如修改控件属性等)
- 在内容页中添加ASP.NET AJAX控件TabContainer,运行出现“未将对象引用设置到对象的实例”错误。
- ASP.NET开发过程中遇到的问题-控件找不到
- asp.net母版页的路径问题
- ASP.Net访问母版页(MasterPage)控件、属性、方法及母版页中调用内容页的方法
- ASP.Net访问母版页(MasterPage)控件、属性、方法及母版页中调用内容页的方法
- asp.net 在使用母版页的子页面cs后台代码中控制母版页中的登录控件显示
- 互联网安全协议漏洞成为国际互联网大会重要议题
- 2014 GCJ Round 1A
- 从零冲击Erlang(一)
- 无法创建Web Application项目的问题
- 2014年4月21日 幼儿园的生意经
- ASP.NET 嵌套母版页中找不到控件问题(未已用到对象错误)
- 删除空格
- Centos系统中 Android tbb的编译
- 重走数据结构之旅
- Jetty学习【六】Connection
- 判断AL是否为十六进制并将其十进制存入AL
- WMI介绍及简单应用
- 基于vxworks的PCI设备驱动编写
- Prototype(原型模型)