Pergunta

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.

  1. Adding another simple UserControl with static text, but it gave me the samme error.
  2. Cleaning and rebuilding my solution.
  3. Changing flightResult to be "new Panel()", but same issue.
  4. Searching the web for people who have had this issue, I found one thread, but with no solution.

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'}
Foi útil?

Solução

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);

}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top