Commenting Your ASP.NET Pages

Published on April 2, 2009 | Filed Under ASP.NET

I came across a very interesting question today and just wanted to write a very quick blog about it.

For those of you who don’t know, there are various degrees of comments, which you can include in your code and ASP.NET pages.

Below is a quick list of what you can use

  • //This is a C# Comment
  • ‘This is a Visual Basic comment (All versions)
  • <!– This is an HTML comment –>
  • <%– This is an ASP.NET comment –%>

The Difference Between HTML and ASP.NET Comments

Take the following code into consideration:

<%@Page Language="C#" AutoEventWireup="true"
 CodeFile="CommentTest.aspx.cs" Inherits="CommentTest" %>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title>Comment Test</title>
    </head>
    <body>
      <form runat="server">
        <p>I have three textboxes here:</p>
        <p><asp:TextBox ID="TextBox1"> runat="server"></asp:TextBox></p>
        <!-- <p><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></p> -->
        <%-- <p><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></p> --%>
      </form>
    </body>
  </html>

When you compile your page, you will only see TextBox1.

Now view your source.

  • TextBox1 is shown (obviously).
  • TextBox2 shows up in HTML comments.
  • TextBox3 is not showing up at all.

Why Did My Textboxes Go?

ASP.NET processes everything on the server’s side before displaying to the client; hence why it’s called server-side code. In any language, comment blocks are the way of telling the code processor to ignore a certain block of text. ASP.NET is no different. Before sending information to the client, it processes everything that you want it to and ignores your comments.

Conclusion

Comment blocks are very useful in any language. Sometimes you need to let future programmers know what’s going on, or maybe you just need to get rid of certain parts while you do your testing. At any rate, comments are valuable tools and I highly encourage you to use them as often as needed!

~Derek Torrence

Leave a Reply

*