Link to home
Start Free TrialLog in
Avatar of roricka

asked on

How can I embed comments in .aspx file so it is compiled out -- ie. not visible on "view source"

In the old asp days,

<% ' a comment %>

could appear anywhere in the <body> of an .aspx file and it would  be invisible to a "view source". Is there a way to do this same thing in .NET (I'm using c#).
Avatar of SteveH_UK
Flag of United Kingdom of Great Britain and Northern Ireland image

You can use a similar tag in ASP.NET

<%-- comment here --%>

Avatar of Jeremy Daley
Jeremy Daley
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of roricka


For some reason, <% -- comment %> doesn't work -- only the c# delimiter does.
roricka:  probably didn't work because of a space after the first % character.

You shouldn't use the // comment in the page layout for several reasons:

1.  It doesn't block the sequence %> which means you can't surround wider blocks of code
2.  It makes your ASPX code language-dependent, which it doesn't need to be
3.  The <% // comments don't work around tags, such as <asp:Button>
4.  The use of <% blocks makes the ASPX code unreadable.
Remember, the sequence is:




The sequence of characters is repeated at both ends.
Avatar of roricka


SteveH UK

Excellent. Now, how do I reassign the points?
You would need to alert a moderator for this zone.  Comments can be made in the Community Support Zone.
what about blocking like this:

<% /*


*/ %>

...cause that's actually the multi-line way of using the forward slash.

multiline comments are useful in code files but they suffer with the same tag issues in an ASPX file.
well honestly, i wouldn't doubt that you're more knowledgeable in this area than i... but i guess i'm a little against the idea that my points need to be stripped from me.
I didn't request the points and I intentionally did not instruct the author to act in any particular way.

Regarding "your points", my suggestion which came first was a) standard, b) documented and c) correct.  The only issue was the author had typed it in wrong, not anything wrong in the suggested solution, so let's not call them "your points" if you don't mind.
i don't really want to argue about who's code was better because i don't doubt that your reasoning is correct. but at the same time, the author must've tried my code with success or else he/she would not have accepted.

i think an assist would be more fair seeing as he/she DID accept my answer and not yours.

secondly, if he/she decided to throw some server-side code into the equation... it wouldn't work with your example.

you might argue that that type of thing was never suggested, but then i could argue that putting the comment into a "block" wasn't either.
Avatar of roricka


Now, now.

Actually, the idea of having language-independent syntax is what I was originally looking for (though didn't specify), and when it appeared as if only the "//" delimiter worked, I scratched my head, but decided it DID satisfy my original request. Had I not been so careless in trying the "--" syntax, I would have split the points.

BTW, for some reason, I can't get to the community support zone w/o pinning my CPU cycles (and on 2 different machines!)

When that calms down, I do plan on splitting the points among all the solutions that satisfied my original question. (That's the way I always do it -- at the point I review the submissions, I don't just give to the first person, but to everyone who satisfied the criteria w/original suggestions.)

That said, it appears that using the "--" approach exclusively in the future is the way to go.

Thanks all.

amabog (roricka)
Firstly, I disagree with your comments (both technically and more generally), but I also don't want to have this discussion.  Secondly, I never made any statement in favour or against an assist.
For clarification:  my most recent comment was directed to daleyjem.

amabog:  glad to help.
that's your prerogative... could you tell me exactly though what it is you disagree with? i'm only curious is all...
what i meant in mentioning server-side was:

    Response.Write("can't be done");

    // comment
    Response.Write("can be done");

it's just more efficient in my opinion than doing this:
    In the code snippet below, I'm going to Write something to the window.
    Response.Write("something to the window.");

still though... i learned a lesson on commenting with <%-- .. i honestly didn't know how to do that, so thanks.

roricka... thanks for the split
The typical use of the <%-- comment block is to wrap existing markup, for example the original page might have:

/* The next line does ......
    and some more comment */
Response.Write("Some content");

Obviously, the typical page would be more complex.  You can use a server-side comment to wrap the complete set:

/* The next line does ......
    and some more comment */
Response.Write("Some content"); %></p>

Which you can't do with an embedded code comment.

The biggest limitation is that none of the ASP.NET comment conventions allow proper comment nesting, but the server-side comment is closest to it.
yeah, .NET can act a little fishy when it comes to certain things. eventhough the general markup of an aspx page seems to follow XML conventions, you couldn't do something like this without an error:

<asp:Table id="theTable" runat="server">
    <asp:TableRow blah blah>blah blah blah</asp:TableRow>
        this is how you comment XML... it'll break your code though if it's in the middle of a .NET control