Can you add child controls to a HtmlGenericControl? Can you add child controls to a HtmlGenericControl? asp.net asp.net

Can you add child controls to a HtmlGenericControl?


"An exception is thrown in the debugger only - the page itself renders as though nothing had happened."

That's fine in that InnerText won't work if there are child controls; that is by design because it doesn't want to render the whole hierarchy of controls at that time for various performance, lifecycle, and side affect reasons.

"... something definitely does happen, as in a larger portion of code, controls are not being rendered as they should."

What does that mean? What is rendering incorrectly in the "larger portion" of code? My guess is that is a problem somewhere else and not related to what you have in your question here.

So I think you're fine. Ignore the exception in InnerText on the parent control. You need to ask a new Question regarding whatever issue you are having with the "larger portion" of your code.


i think an easy solution to avoid this error can be to render the child HtmlGenericControl into a string and then adding it to the parents inside the InnerHtml attribute. something like this:

HtmlGenericControl parent = new HtmlGenericControl("div");HtmlGenericControl child = new HtmlGenericControl("div");StringWriter stringWriter = new StringWriter();HtmlTextWriter twr = new HtmlTextWriter(stringWriter);child.RenderControl(twr);string rendered=stringWriter.ToString().Replace("\r","").Replace("\n","").Replace("\t","");parent.InnerHtml =  rendered;


you need to use a panel first. then you can create child HtmlGenericControl and then parent HtmlGenericControl and then you can add the parent control into the panel. for example:

<asp:Panel ID="panel" runat="server" ></asp:Panel>//parentHtmlGenericControl div = new HtmlGenericControl("div"); //child HtmlGenericControl p = new HtmlGenericControl("p");      // adding child control to parent control.div.Controls.Add(p);div.Visible = true;//adding parent control to panelpanel.Controls.Add(div);