Share via


Code Render Blocks

Defines inline code or inline expressions that execute when the page is rendered. There are two styles: inline code and inline expressions. Use inline code to define self-contained code blocks or control flow blocks.

<% inline code %>

Use inline expressions as a shortcut for calling the HttpResponse.Write method.

<%=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 fragment will cause an error:

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

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

<%@ page language="C#" %>
   <%
      String s = "%" + ">";
      Response.Write(s);
   %>
[Visual Basic]
<%@ 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 example shows how you can use the 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>
<% } %>
[Visual Basic]
<% For I=0 to 9 %>
     <font size="<%=i%>"> Hello World! </font>
<% Next %>

See Also

Introduction to Web Forms Pages | Web Forms Syntax