Tip/Trick: Using Server Side Comments with ASP.NET 2.0


You are coding away on an ASP.NET page, and are trying to isolate a problem within the page.  You have some existing html/controls/markup/in-line code that is being used on the page, and you want to temporarily comment it out while you fix the problem.


ASP.NET supports a little known feature called “server-side comments” that you can use to completely disable code/controls/html in a page.  Server-side comments in ASP.NET are delimited using a <%– –%> syntax.  For example:


Commented out HTML/CODE/Markup.  Anything with

this block will not be parsed/handled by ASP.NET.

<asp:Calendar runat=”server”></asp:Calendar>

<%# Eval(“SomeProperty”) %>


One common question people ask is what the difference is between using client-side HTML comments and server-side comments.  The key difference is that with client-side comments it is the browser which is ignoring the content within them.  Code/controls within client-side comments will still be executed on the server and sent down to the browser.  As such, if there is a server error caused within them it will block running the page.

With server-side comments, the ASP.NET compiler ignores everything within these blocks at parse/compile time, and removes the content completely when assembling the page (like its contents weren’t there at all).  Consequently, any errors caused by mal-formed controls or issues with inline code or data-binding expressions within them will be ignored.  The page is also just as fast with controls/code within server-side comments as if there were no controls/code on the page at all (there is no runtime performance overhead to them).

One tip/trick to take advantage of in the HTML source editor within Visual Web Developer and VS 2005 is the automatic comment/uncomment feature they support.  You can use this by selecting some markup within a .aspx page, and then clicking the “comment” command button that is on HTML Source Editor Toolbar:

This will automatically wrap the selected content with a <%– –%> block.  You can likewise move the cursor within it and click the uncomment command to remove the comment.  Keyboard short-cuts are also automatically enabled to do this purely from the keyboard.  The exact keystrokes depend on what VS profile you have configured – but on my system it is “Ctrl-K, Ctrl-C” to comment a block and “Ctrl-K, Ctrl-U” to uncomment one.

Note that this comment/uncomment command in VS works not only in HTML – but also within regular VB/C# source files as well.  It provides an easy and consistent way to comment out functionality everywhere within your project.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: