Can I place a comment inside a tag in ASP.NET?

I am looking to display this in my .aspx page, without using special character XML tags, can this be achieved?

<asp:ServerTag Property1="a"
    Property2="b"
    Property3="c" <%-- Comment why this particular property is necessary --%>
    Property4="d" />

However, I am greeted with the error message Server tags cannot contain <% ... %> constructs. If I use an HTML <!– –> tag, I’m told the server tag is not well formed.

Is there any other syntax to make this possible?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Put server-side comment above your server-side control.

  • <!-- client-side comment (html) – appears in html source but not rendered on page
  • <%-- server-side comment – stripped out on server, never sees light of day, browser never knows about it

like this

<%-- Usage:
Property2 is xyz... 
Property3 will .. abc. Ignore Property  1 when this is set. etc
--%>
<asp:ServerTag Property1="a"
    Property2="b"
    Property3="c" 
    Property4="d" />

It’s just like putting source code comments above your functions.
 

Think “server to server”. It will make the difference between your HTML source looking like
cluttered with “pass through” html comment <!--:

<!– Property usage: abc, def, …xyz –>
Rendered server control contents.

vs. the cleaner stripped out ” <%-- source:

Rendered server control contents.

Less bandwidth with latter too. No extraneous (and confusing to user) comments in HTML source.

Method 2

It’s not possible, no. The server tags need to be well-formed XML and you can’t have tags like that in XML. You can put a comment at the top, of course, like so:

<!-- Property2 needed because... -->
<asp:ServerTag Property1="a" Property2="b" Property3="c" />

Method 3

Not necessarily like that but you may want to consider decorating the property in c# to let the user know its relevance. After that something like resharper (or maybe vs) will give you this information when you try to set it.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x