c# - 如果在 Page.Load 中动态添加,则 UserControl 不会出现在页面上

标签 c# asp.net user-controls asp.net-4.0

我有一个自定义 ASP.NET 用户控件,已通过 Page.Load 方法添加到网页上的面板中。该控件包含一些标签和一个 GridView。我使用以下代码将其添加到页面:

ReportingControl rc = new ReportingControl(this.Username, this.SoaContext, transactionId, p.PaymentTypeRequestKey);
this.pnlPB.Controls.Add(rc);

由于某种原因,添加的控件实际上并未显示在页面上。我还注意到控件中的控件都是空的,但我不知道这是否是问题的一部分,或者它们是否是稍后创建的。有什么想法吗?

最佳答案

它是UserControl还是继承自CompositeControlWebControl等的自定义服务器控件?

通常对于用户控件,您可以通过ascx位置加载它们,例如:

//load the control
Control rc= LoadControl("~/UserControls/MyControl.ascx");

//set up values as needed
rc.Username = this.Username; 

//add the control where needed
this.pnlPB.Controls.Add(rc);

关于c# - 如果在 Page.Load 中动态添加,则 UserControl 不会出现在页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3215393/

相关文章:

c# - 我们可以在一个表单中有多个提交类型按钮吗?

c# - 如何使用 WPF 中的按钮/代码隐藏实例同步或访问 2 个 UserControl?

c# - 在 Unity 中通过代码创建枢轴点

c# - phantomjsdriver 如何添加用户代理?

c# - 修改 list 文件 Win32 Visual Studio 2022

c# - 将接口(interface)映射到不实现接口(interface) C# 的类(动态)

asp.net - 关闭.net服务中的所有功能

c# - asp.net 页面中的 Cursor.Position

visual-studio-2008 - 如何阻止VS2008不必要地修改CSPROJ文件?

c# - 如何在 C# 中向 UserControl 添加事件?