Code Render Blocks

Code render blocks define inline code or inline expressions that execute when the page is rendered. There are two styles of code render blocks: inline code and inline expressions. Use inline code to define self-contained lines or blocks of code. Use inline expressions as a shortcut for calling the Write method.

<% inline code %>
<%=inline expression %>

Remarks

A compilation error occurs if you try to include the character sequence %> anywhere inside a code render block. That sequence can only be used to close the code render block. For example, the following code will cause an error:

<%@ page language="C#" %>
   <%
      Response.Write(" %>");
   %>
<%@ page language="VB" %>
   <%
      Response.Write("%>)
   %>

To work around this error, you can build a string containing the sequence of characters, as in the following code example:

<%@ page language="C#" %>
   <%
      String s = "%" + ">";
      Response.Write(s);
   %>
<%@ page language="VB" %>
    <%
      Dim s as String 
      s = "%" & ">"
      Response.Write(s)
    %>

Note

Unlike Active Server Pages (ASP), in ASP.NET it is invalid to declare a function or subroutine within a code render block (between <% and %> tags).

Example

The following code example shows how you can use the code render blocks to display the same HTML text in a number of different font sizes.

<% for (int i=0; i<10; i++) { %>
     <font size="<%=i %>"> Hello World! </font>
<% } %>
<% Dim I as Integer
   For I=0 to 9 %>
     <font size="<%=i%>"> Hello World! </font>
<% Next %>

See Also

Concepts

ASP.NET Web Page Syntax Overview

Introduction to Programming ASP.NET Web Pages