227

Is there a way to comment out markup in an .ASPX page so that it isn't delivered to the client? I have tried the standard comments <!-- --> but this just gets delivered as a comment and doesn't prevent the control from rendering.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
MikeJ
  • 14,430
  • 21
  • 71
  • 87

8 Answers8

353
<%--
            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”) %>     
--%>

Source

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
75

Bonus answer: The keyboard shortcut in Visual Studio for commenting out anything is Ctrl-KC . This works in a number of places, including C#, VB, Javascript, and aspx pages; it also works for SQL in SQL Management Studio.

You can either select the text to be commented out, or you can position your text inside a chunk to be commented out; for example, put your cursor inside the opening tag of a GridView, press Ctrl-KC, and the whole thing is commented out.

Herb Caudill
  • 50,043
  • 39
  • 124
  • 173
31

FYI | ctrl + K, C is the comment shortcut in Visual Studio. ctrl + K, U uncomments.

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Matthew M. Osborn
  • 4,673
  • 4
  • 25
  • 26
27
<%-- not rendered to browser --%>
Sklivvz
  • 30,601
  • 24
  • 116
  • 172
13

I believe you're looking for:

<%-- your markup here --%>

That is a serverside comment and will not be delivered to the client ... but it's not optional. If you need this to be programmable, then you'll want this answer :-)

Community
  • 1
  • 1
Joel Martinez
  • 46,929
  • 26
  • 130
  • 185
12

Yes, there are special server side comments:

<%-- Text not sent to client  --%>
Mike G
  • 4,232
  • 9
  • 40
  • 66
stefano m
  • 4,094
  • 5
  • 28
  • 27
9

While this works:

<%-- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ht_tv1.Default" %> --%>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Blank._Default" %>

This won't.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" <%--Inherits="ht_tv1.Default"--%> Inherits="Blank._Default" %>

So you can't comment out part of something which is what I want to do 99.9995% of the time.

ggb667
  • 1,881
  • 2
  • 20
  • 44
5

Another way assuming it's not server side code you want to comment out is...

<asp:panel runat="server" visible="false">
    html here
</asp:panel>
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
BigJump
  • 15,561
  • 3
  • 31
  • 29
  • 1
    I did delete this answer as its not strictly 'commenting out'. However, Joel's answer refers to mine so for the time being I've undeleted it. – BigJump Sep 23 '08 at 14:46
  • 1
    asp:PlaceHolder does the same but is designed to not generate any additional HTML. – billpg Sep 22 '11 at 14:45