Miscellaneous Debris

Avner Kashtan's Frustrations and Exultations
LoadControl and HTML Comments

Developin webparts with many UI elements can be a hassle - which is why I'm fond of the UserControl shortcut - I build my web UI as a WebUserControl, then simply have my webpart call Page.LoadControl to dynamically load that usercontrol.

A problem I've encountered with it today is that LoadControl, strangly enough, ignores HTML comment in the ASCX file.

Let's say I have the following code in the ASCX:

<!-- <asp:Repeater runat="server" id="Repeater1" /> -->
<asp:Repeater runat="server" id="Repeater1" />

This should instantiate only one Repeater object - and does, when the ASCX is hosted normally inside an ASPX page.
When I load it dynamically, though, I get an exception in LoadControl stating that "Parse Error: A control named Repeater1 is already defined" - meaning that for some reason it parses the first control too, even though it's commented out.
If I erase the commented section, it runs as planned.

Anyone know why it ignores my markup?

 

Published Thursday, August 19, 2004 7:45 PM by AvnerK

Filed under: ,

Comments

# re: LoadControl and HTML Comments@ Thursday, August 19, 2004 1:05 PM

There are special ASP.NET HTML comments that you need to use.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconserver-sidecomments.asp

Scott

# re: LoadControl and HTML Comments@ Thursday, August 19, 2004 1:37 PM

yeah, use the ASP.Net comment markers instead, the parser completely ignores the inside, even to the point of not emitting the comments out to the browser.

<%!-- comments --%>

Eric Newton

# re: LoadControl and HTML Comments@ Friday, August 20, 2004 10:32 AM

Presumably ASP.NET treats the tokens "<!--" and "-->" just like any other literal HTML. They are a message saying "ignore this", but it is a message to the *browser*, not to the ASP.NET engine. This enables you to use server controls to generate the contents of HTML comments that get emitted to the page, if you so desire.

To have ASP.NET itself ignore something, put it between the ASP.NET comment tokens "<%--" and "%>".

I believe the syntax in the previous comment is incorrect: the exclamation mark should not be there, and the two dashes in the end token are not needed.

Gunnlaugur Thor Briem

# re: LoadControl and HTML Comments@ Saturday, August 21, 2004 3:19 AM

Thanks for the info, guys.
My question is - why does the same commented ASCX work when embedded in an ASPX page, but fail when loaded via LoadControl?

Avner Kashtan

# re: LoadControl and HTML Comments@ Wednesday, August 25, 2004 9:05 PM

No idea why that is. But I just checked back to correct myself: the two dashes in the ASP.NET comment end token *are* needed. The VS.NET syntax highlighter just doesn't know that, apparently :)

My apologies for stating without testing! (a.k.a. talking out of my hind side)

Gunnlaugur Thor Briem

Leave a Comment

(required) 
(required) 
(optional)
(required)