详解ASP.NET注释语句及服务器注释

开发 后端
本文将介绍ASP.NET注释语句,包括服务器端的注释语句。良好的注释能帮助开发人员更好的理解程序代码,希望本文能对大家有所帮助。

服务器端ASP.NET注释语句

允许您将ASP.NET注释语句包括在 .aspx 文件的正文中。服务器端注释元素的开始标记和结束标记之间的任何内容,不管是 ASP.NET 代码还是文本,都不会在服务器上进行处理或呈现在结果页上。

  1. <%-- commented out code or content --%> 

备注

ASP.NET服务器端注释块与传统的语言特定注释块具有相同的用法(包括文档和测试)。

和 <% %>块中,可以使用正用于编码的语言的注释语法。

如果在 <% %>块中使用服务器端ASP.NET注释块,则会出现编译错误。

开始和结束ASP.NET注释标记可以出现在同一行代码中,也可以由许多被注释掉的行隔开。

服务器端注释块不能被嵌套。

ASP.NET注释示例

下面的示例说明被注释掉的 HtmlButton 控件。

  1. <%-- <button runat="server" id="MyButton" OnServerClick="MyButton_Click"> 
  2.              Click here for enlightenment!  
  3.        button> 
  4.  --%>  

JScript 注释

单行 JScript 注释以两个正斜杠 (//) 开始。以下是单行注释(后跟一行代码)的一个示例。

  1. // This is a single-line comment.  
  2. aGoodIdea = "Comment your code for clarity.";  
  3. 多行 JScript 注释以正斜杠和星号 (/*) 开头,以相反的顺序 (*/) 结束。  
  4. /*  
  5. This is a multiline comment that explains the preceding code statement.  
  6. The statement assigns a value to the aGoodIdea variable. The value,  
  7. which is contained between the quote marks, is called a literal. A  
  8. literal explicitly and directly contains information; it does not  
  9. refer to the information indirectly. The quote marks are not part  
  10. of the literal.  
  11. */ 

如果试图在一个多行注释中嵌入另一个多行注释,JScript 将以一种意想不到的方式解释生成的多行注释。标记嵌入的多行注释结尾的 */ 将被解释为整个多行注释的结尾。因此,在嵌入的多行注释后面的文本将被解释为 JScript 代码,并可能生成语法错误。

在下面的示例中,由于 JScript 将最里面的 */ 解释为最外面注释的结尾,因此第三行文本将被解释为 JScript 代码:

  1. /* This is the outer-most comment  
  2. /* And this is the inner-most comment */ 
  3. ...Unfortunately, JScript will try to treat all of this as code. */ 

建议将所有ASP.NET注释语句编写为单行注释的块。这样就允许随后用一个多行ASP.NET注释来注释大段代码。

  1. // This is another multiline comment, written as a series of single-line comments.  
  2. // After the statement is executed, you can refer to the content of the aGoodIdea  
  3. // variable by using its name, as in the next statement, in which a string literal is  
  4. // appended to the aGoodIdea variable by concatenation to create a new variable.  
  5. var extendedIdea = aGoodIdea + " You never know when you'll have to figure out what it does."

或者还可以使用条件编译安全有效地注释大段代码。

【编辑推荐】

  1. 浅析ASP.NET Web 安全性
  2. ASP.NET的Session State
  3. 浅析IsPostBack属性ASP.NET
  4. ASP.NET构架与安全机制
  5. 概述ASP.NET水晶报表
责任编辑:彭凡 来源: CSDN
相关推荐

2009-07-29 13:42:25

ASP.NET注释

2009-08-04 13:10:05

ASP.NET服务器控

2009-08-10 14:08:15

ASP.NET服务器控ASP.NET组件设计

2009-08-04 17:18:37

2011-07-12 15:17:02

ASP.net服务器控件

2009-08-03 18:00:00

ASP.NET服务器控

2009-08-10 14:25:33

ASP.NET服务器控

2009-07-28 10:43:23

Web服务器ASP.NET

2009-08-04 15:13:38

2009-08-04 13:39:43

ASP.NET 2.0

2009-07-27 12:56:27

控件CheckBoxLASP.NET服务器

2009-08-06 14:16:04

ASP.NET服务器控

2009-07-29 16:08:07

ASP和ASP.NET

2009-07-29 12:55:44

ASP.NET身份验证

2009-08-01 10:07:58

ASP.NET服务器控ASP.NET

2011-07-12 15:03:28

2009-08-05 18:47:55

ASP.NET服务器

2009-08-01 20:59:08

ASP.NET服务器控ASP.NET服务器ASP.NET

2009-08-06 14:42:54

ASP.NET服务器控

2009-07-22 16:25:41

ASP.NET AJA
点赞
收藏

51CTO技术栈公众号