asp.netuser-controlsfindcontrolcontentplaceholder

How to find user control of an ASP.NET page inside of event of another user control on that ASP.NET page EDIT: different content placeholders?


I have a ASP.NET page with 2 user controls registered. The first one has only one button in it. The second one is simple text and hidden on default. What I want is to make the second one visible when the button in the first one is clicked (that is on button click event).

ASP.NET page:

<%@ Page Title="" Language="C#" CodeFile="test.aspx.cs" Inherits="test" %>
<%@ Register Src="~/UC_button.ascx" TagName="button" TagPrefix="UC" %>
<%@ Register Src="~/UC_text.ascx" TagName="text" TagPrefix="UC" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MyTestContent" Runat="Server">
    <UC:button ID="showbutton1" runat="server" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MyTestContent2" Runat="Server">
    <UC:text runat="server" Visible="false" ID="text1" />
</asp:Content>

UC_Button.ascx.cs:

protected void button1_Click(object sender, EventArgs e)
{
    Button btnSender = (Button)sender;
    Page parentPage = btnSender.Page;
    UserControl UC_text = (UserControl)parentPage.FindControl("text1");
    UC_text.Visible = true;
}

What am I doing wrong? I get well known Object reference not set to an instance of an object. error on that last line of the code.

EDIT:

One thing I forgot to mention when first posting this. User controls are in different <asp:Content></asp:Content> controls (I edited upper example). If I put them in the same placeholder code works just fine. If I put them in the separate content placeholders I can't find them in any way with findcontrol. Why is that and how can I find them?


Solution

  • Ok I found solution until better one comes my way. The problem is, as Jamie Dixon pointed out (thank you Jamie):

    The FindControl method does not do a deep search for controls. It looks directly in the location you specify for the control you're requesting.

    So because I have user controls in different contentplaceholders I must first find targeted placeholder (where the user control reside) and then I can search for user control inside it:

    protected void Dodaj_Feed_Panel_Click(object sender, EventArgs e)
        {
            ContentPlaceHolder MySecondContent = (ContentPlaceHolder)this.Parent.Parent.FindControl("MyTestContent2");
    
            UserControl UC_text = (UserControl)MySecondContent.FindControl("text1");
            UC_text.Visible = true;
        }
    

    what really annoys and confuses me is the this.Parent.Parent part because I know it's not the best solution (in case I change the hierarchy a bit this code will break). What this part of code actually does is that it goes two levels up in page hierarchy (that is page where both user controls are). I don't know what the difference with this.Page is because for me it means the same but is not working for me.

    Long term solution would be something like server side "jQuery-like selectors" (it can found elements no matter where they are in hierarchy). Anyone has a better solution?