UPDATE: Closing, found out that even though I get that error in debugging, it is actually added to the Controls collection, but still annoying that I can't see it as I usually would.
This code used to work fine, but for some reason it has started to fail. It is only when I try to add a UserControl to "flightResult" that it fails. If I use .NET controls it works just fine.
Things I have tried.
The only change recently really is that I installed IIS 7.0 - can that have anything to do with this?
My code:
var flightResult = new HtmlGenericControl("div");
var flightProduct = (FlightProduct) LoadControl("~/UserControls/FlightProduct.ascx");
//finalResult.InnerText = ""
finalResult.Controls.Add(flightProduct);
//finalResult.InnerText = {InnerText = '((System.Web.UI.HtmlControls.HtmlContainerControl)(finalResult)).InnerText' threw an exception of type 'System.Web.HttpException'}
It work for me, try with this.
User Control MarkUp
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Control1.ascx.cs" Inherits="Control" %>
<asp:Label ID="lblTest" runat="server" Text="This is a test"></asp:Label>
Default.aspx Page Code Behind
const string path = "~/Control1.ascx";
HtmlGenericControl div;
protected void Page_Load(object sender, EventArgs e){
div = new HtmlGenericControl("div");
div.Attributes.Add("runat", "server");
var userControl = LoadControl(path);
if (userControl != null)
div.Controls.Add(userControl);
this.form1.Controls.Add(div);
}