Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6209349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:58:07+00:00 2026-05-24T05:58:07+00:00

I have a ASP.NET page with 2 user controls registered. The first one has

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:58:08+00:00Added an answer on May 24, 2026 at 5:58 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET page which has a button it it. The button click
I have a ASP.NET page with an asp:button that is not visible. I can't
I have an ASP.NET page which has a script manager on it. <form id=form1
I have an asp.net page with a button. This button generates and inserts a
In my current project (ASP.Net 2.0) I have a lot of web user controls,
I'm converting a classic ASP page to ASP.NET MVC 2. I have a simple
I have an ASP.NET page with two instances of the same Web User Control
I have the user controls (ASP.NET 3.5) implemented and noticed that each user is
I have say this hierarchy in ASP.NET: page user control 1 user control 2
I have a asp.net page, and would like to know whether script1 is already

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.