I created a new 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.
Why is this occurring?
Use the relative path overload of the LoadControl method instead, as noted here.
http://msdn.microsoft.com/en-us/library/ewtd66a0.aspx
Edit: changed answer after research.