如何在该ASP.NET页面上的另一个用户控件的事件内找到ASP.NET页面的用户控件编辑:不同的内容占位符?

Jan*_*nez 3 asp.net user-controls findcontrol contentplaceholder

我有一个ASP.NET页面,注册了2个用户控件.第一个只有一个按钮.第二个是简单文本,默认隐藏.我想要的是在单击第一个按钮时(即按钮单击事件)使第二个可见.

ASP.NET页面:

<%@ 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>
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我Object reference not set to an instance of an object.在代码的最后一行得到了众所周知的错误.

编辑:

首次发布此内容时,我忘记提及一件事.用户控件在不同的<asp:Content></asp:Content>控件中(我编辑了上面的示例).如果我把它们放在相同的占位符代码中工作就好了.如果我将它们放在单独的内容占位符中,我无法使用findcontrol以任何方式找到它们.为什么这样,我怎么能找到它们?

Sau*_*abh 6

请检查以下内容:

UserControl UC_text = (UserControl)this.NamingContainer.FindControl("text1");
Run Code Online (Sandbox Code Playgroud)