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

 

Problem

 

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.

 

Solution

 

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.

 

Hope this helps,

 

Scott

 

Published Sunday, July 9, 2006 10:09 AM by ScottGu

Comments

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

Sunday, July 9, 2006 2:27 PM by John S.
I always wondered if there was a way to do that. Thanks!

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

Sunday, July 9, 2006 3:41 PM by Joern Schou-Rode
WHAT!? Here are have been changing runat="server" to Xrunat="server" all of the time to comment out controls in my forms. Thanks for sharing this very useful trick :)

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

Sunday, July 9, 2006 8:24 PM by foobar

Heh.  I figured this out a couple of years ago.  What's funny is that it's almost totally undocumented - even O'Reilly's Programming ASP.NET book didn't mention it.

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

Monday, July 10, 2006 1:21 AM by vikram lakhotia
Yes Its a really cool feature. I have been using this while working with the studio. Its really helpful when I am debugging some code

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

Monday, July 10, 2006 1:35 PM by foobar

Scott, is there any way to get this to work with CSS files?

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

Monday, July 10, 2006 5:49 PM by infosrama

really a great feature, thanks for sharing.

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

Monday, July 10, 2006 11:22 PM by ScottGu

Hi Foobar,

.css files aren't by default handled by ASP.NET - so this doesn't directly work there.

But you could build a CSS handler for ASP.NET if you wanted to, and implement a server commenting scheme within that.

Hope this helps,

Scott

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

Tuesday, July 11, 2006 2:44 AM by foobar

Sorry Scott, I meant getting VS2005 to surround CSS text with CSS a comment block.  Strictly editor functionality I'm talking about.

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

Tuesday, July 11, 2006 11:22 AM by ScottGu

Hi Foobar,

Commenting works for CSS defined within <style> blocks within the page (it wraps a server-side comment around the regions selected within it), but not for external CSS files (with those you need to add the comment manually).

Commenting does also work in Javascript files and code.

Hope this helps,

Scott

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

Tuesday, July 11, 2006 7:20 PM by Simon Owen
Hi Scott, Thanks for another great post! Sorry this is only a mildly related question, but is there any way to get comment items in HTML to show in the task list (//TODO etc.)?

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

Tuesday, July 11, 2006 11:10 PM by ScottGu

Hi Simon,

Unfortunately I don't think there is anyway to get HTML comments to show up in the task list (at least not that I've been able to find yet).

Sorry!

Scott

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

Wednesday, February 21, 2007 7:44 PM by Dave Bartlett

This has to be the best little piece of ASP.NET advice I've heard all year! :)