Why do my dynamically added user controls show that their ascx controls are null?

I created a new TestControl control. At the front end I gave him

 <asp:Label ID="lblTest" runat="server" /> 

On server:

 public partial class TestControl : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { lblTest.Text = "blah"; } } 

When I load the control with:

  var control1 = LoadControl(typeof(TestControl), null); Controls.Add(control1); 

I get an exception that lblTest is null.

Why is this happening?

+4
source share
1 answer

Use the relative path overload of the LoadControl method, as indicated here.

http://msdn.microsoft.com/en-us/library/ewtd66a0.aspx

Edit: changed the answer after research.

+4
source

Source: https://habr.com/ru/post/1400248/


All Articles