我正在尝试在User Control(ascx)
中的内容页面上找到标签 Page p = this.Page;
//this line causes application to unload with no exception
ContentPlaceHolder cp = (ContentPlaceHolder)p.Master.FindControl("Content2");
Label label = (Label)cp.FindControl("SomeLabel");
它只是卸载自己,没有异常消息。为什么会这样?
答案 0 :(得分:0)
我能够让它工作,以便在我的用户控件所在的Label
内找到ContentPlaceHolder
:
<强> Site1.Master:强>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
<强> Default.aspx的:强>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestWeb._Default" MasterPageFile="~/Site1.Master" %>
<%@ Register Src="WebUserControl1.ascx" TagName="stuff" TagPrefix="uc" %>
<asp:Content ID="indexContent" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Label ID="Label1" Text="Label" runat="server" />
<uc:stuff ID="test" runat="server" />
</asp:Content>
<强> WebUserControl1.ascx.cs:强>
protected void Page_Load(object sender, EventArgs e)
{
if (Page.Master != null)
{
Control c = Page.Master.FindControl("ContentPlaceHolder1");
if (c != null)
{
Label l = (Label)c.FindControl("Label1");
}
}
}