詳解ASP.NET注釋語句及服務(wù)器注釋
服務(wù)器端ASP.NET注釋語句
允許您將ASP.NET注釋語句包括在 .aspx 文件的正文中。服務(wù)器端注釋元素的開始標(biāo)記和結(jié)束標(biāo)記之間的任何內(nèi)容,不管是 ASP.NET 代碼還是文本,都不會在服務(wù)器上進行處理或呈現(xiàn)在結(jié)果頁上。
- <%-- commented out code or content --%>
備注
ASP.NET服務(wù)器端注釋塊與傳統(tǒng)的語言特定注釋塊具有相同的用法(包括文檔和測試)。
在 和 <% %>塊中,可以使用正用于編碼的語言的注釋語法。
如果在 <% %>塊中使用服務(wù)器端ASP.NET注釋塊,則會出現(xiàn)編譯錯誤。
開始和結(jié)束ASP.NET注釋標(biāo)記可以出現(xiàn)在同一行代碼中,也可以由許多被注釋掉的行隔開。
服務(wù)器端注釋塊不能被嵌套。
ASP.NET注釋示例
下面的示例說明被注釋掉的 HtmlButton 控件。
- <%-- <button runat="server" id="MyButton" OnServerClick="MyButton_Click">
- Click here for enlightenment!
- < SPAN>button>
- --%>
JScript 注釋
單行 JScript 注釋以兩個正斜杠 (//) 開始。以下是單行注釋(后跟一行代碼)的一個示例。
- // This is a single-line comment.
- aGoodIdea = "Comment your code for clarity.";
- 多行 JScript 注釋以正斜杠和星號 (/*) 開頭,以相反的順序 (*/) 結(jié)束。
- /*
- This is a multiline comment that explains the preceding code statement.
- The statement assigns a value to the aGoodIdea variable. The value,
- which is contained between the quote marks, is called a literal. A
- literal explicitly and directly contains information; it does not
- refer to the information indirectly. The quote marks are not part
- of the literal.
- */
如果試圖在一個多行注釋中嵌入另一個多行注釋,JScript 將以一種意想不到的方式解釋生成的多行注釋。標(biāo)記嵌入的多行注釋結(jié)尾的 */ 將被解釋為整個多行注釋的結(jié)尾。因此,在嵌入的多行注釋后面的文本將被解釋為 JScript 代碼,并可能生成語法錯誤。
在下面的示例中,由于 JScript 將最里面的 */ 解釋為最外面注釋的結(jié)尾,因此第三行文本將被解釋為 JScript 代碼:
- /* This is the outer-most comment
- /* And this is the inner-most comment */
- ...Unfortunately, JScript will try to treat all of this as code. */
建議將所有ASP.NET注釋語句編寫為單行注釋的塊。這樣就允許隨后用一個多行ASP.NET注釋來注釋大段代碼。
- // This is another multiline comment, written as a series of single-line comments.
- // After the statement is executed, you can refer to the content of the aGoodIdea
- // variable by using its name, as in the next statement, in which a string literal is
- // appended to the aGoodIdea variable by concatenation to create a new variable.
- var extendedIdea = aGoodIdea + " You never know when you'll have to figure out what it does.";
或者還可以使用條件編譯安全有效地注釋大段代碼。
【編輯推薦】