C# 如何从用户控件中查找占位符控件

C# 如何从用户控件中查找占位符控件,c#,C#,我有两个名为UC_1和UC_2的用户控件名 在UC_1中,我有一个占位符。我想引用UC_2中的占位符 我尝试了以下代码,但出现空错误: string userControlName = "UC_1.ascx"; System.Web.UI.UserControl userControl = null; PlaceHolder objPlaceHolder = ((TimeReportUC)Page.FindControl("placeHolderID")).FindControl("place

我有两个名为UC_1和UC_2的用户控件名

在UC_1中,我有一个占位符。我想引用UC_2中的占位符

我尝试了以下代码,但出现空错误:

string userControlName = "UC_1.ascx";
System.Web.UI.UserControl userControl = null;

PlaceHolder objPlaceHolder = ((TimeReportUC)Page.FindControl("placeHolderID")).FindControl("placeHolderID") as PlaceHolder;
                userControl = (System.Web.UI.UserControl)Page.LoadControl(userControlName);
                objPlaceHolder.Controls.Add(userControl);

用户控件访问其他用户控件不是一个好的设计实践。为什么需要这个功能?这很容易做到,但可能会把数据和gui混淆,这不是一件好事。当一个用户控件寻址另一个的元素时,这称为紧密耦合。如果你不介意,你可以简单地创建一个你想要的静态实例。我经常将UserControls注入作为占位符的ContentControls,只需创建一个接受用户控件的界面。只需注入你想要的。否则,您可以触发事件来发送数据。是否有人可以分享我的做法?