[英]Why are my dynamically added user controls showing having their ascx controls as null?
I created a new Control TestControl
. 我创建了一个新的Control
TestControl
。 On the front-end I gave it 在前端我给了它
<asp:Label ID="lblTest" runat="server" />
On the backend: 在后端:
public partial class TestControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
lblTest.Text = "blah";
}
}
When I load the control via: 当我通过以下方式加载控件时:
var control1 = LoadControl(typeof(TestControl), null);
Controls.Add(control1);
I get an exception that lblTest
is null. 我得到一个例外,
lblTest
为null。
Why is this occurring? 为什么会这样?
Use the relative path overload of the LoadControl method instead, as noted here. 请使用LoadControl方法的相对路径重载,如此处所述。
http://msdn.microsoft.com/en-us/library/ewtd66a0.aspx http://msdn.microsoft.com/en-us/library/ewtd66a0.aspx
Edit: changed answer after research. 编辑:研究后改变了答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.