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?
please check below:
UserControl UC_text = (UserControl)this.NamingContainer.FindControl("text1");