ASP.NET Server-side comments

I believe a good number of you know about Server-side commenting. This blog is just like a revival to refresh your memories.

When you write comments in your .aspx/.ascx files, people usually write them as:

   1: <!-- This is a comment. -->

To show that it actually makes a difference for using the server-side commenting technique, I’ve started a web application project and my default.aspx page looks like this:

   1: <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ServerSideComment._Default" %>
   2: <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
   3: </asp:Content>
   4: <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
   5:     <h2>
   6:         <!-- This is a comment -->
   7:         Welcome to ASP.NET!
   8:     </h2>
   9:     <p>
  10:         To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
  11:     </p>
  12:     <p>
  13:         You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
  14:             title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
  15:     </p>
  16: </asp:Content>

See the comment in line 6 and when I run the app, I can do a view source on the browser which shows up as:

   1: <h2>
   2:     <!-- This is a comment -->
   3:     Welcome to ASP.NET!
   4: </h2>

Using Fiddler shows the page size as:

screen

Let’s change the comment style and use server-side commenting technique.

   1: <h2>
   2:     <%-- This is a comment --%>
   3:     Welcome to ASP.NET!
   4: </h2>

Upon rendering, the view source looks like:

   1: <h2>
   2:     
   3:     Welcome to ASP.NET!
   4: </h2>

Fiddler now shows the page size as:

screen1

The difference is that client-side comments are ignored by the browser, but they are still sent down the pipe. With server-side comments, the compiler ignores everything inside this block.

Visual Studio’s Text Editor toolbar also puts comments as server-side ones. If you want to give it a shot, go to your design page and press Ctrl+K, Ctrl+C on some selected text and you’ll see it commented in the server-side commenting style.

Published Saturday, April 17, 2010 6:52 AM by nmarun
Filed under: ,

Comments

# re: ASP.NET Server-side comments

Friday, April 16, 2010 8:46 PM by David Taylor

Hi,

Also client side comments cannot be used within certain ASP.NET controls (and throw a cannot include literal exception), but you can use server side comments anywhere in your ASP.NET page.

Regards,

David

# re: ASP.NET Server-side comments

Friday, April 16, 2010 10:24 PM by nmarun

Thanks for the update Dave.

Arun

# re: ASP.NET Server-side comments

Saturday, April 17, 2010 7:53 PM by Phil

Some else recently posted a blog calling client-side comments evil; I forgot the name of the blogger though. Anyway, it is nice that you posted data to back up the fact that client side comments are evil.

# re: ASP.NET Server-side comments

Wednesday, April 28, 2010 12:13 AM by nmarun

Phil, good you liked it. Yes, I had read it quite some time back and had almost forgotten about it until recently I read about. Decided to blog it so it'll register better in my memory.

Arun

# Client-side or Server-side Comments?

Monday, May 03, 2010 12:04 PM by Client-side or Server-side Comments?

Pingback from  Client-side or Server-side Comments?

# re: ASP.NET Server-side comments

Saturday, May 08, 2010 10:48 PM by pankaj

very good....

# re: ASP.NET Server-side comments

Tuesday, August 09, 2011 4:24 AM by The North Face Canada

Thanks very much for this wonderful blog;this is the kind of thing that keeps me going through the day.

Leave a Comment

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