views:

40

answers:

1

I am trying to locate a label on a content page from within 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");

It just unloads itself with no exception mesage. Why does it happen?

A: 

I was able to get this to work so that it finds a Label from within a ContentPlaceHolder where my user control resides:

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");
        }
    }
}
CAbbott
Thanks for reply. I have this code on a property setter of ascx that maybe the problem.
Victor