Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
ASP.NET Reference
Global.asax Syntax
 Code Render Blocks

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework General Reference
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(String) method.

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

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:

C#
<%@ 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 code example:

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

Visual Basic
<%@ page language="VB" %>
    <%
      Dim s as String 
      s = "%" & ">"
      Response.Write(s)
    %>
NoteNote:

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).

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.

C#
<% for (int i=0; i<10; i++) { %>
     <font size="<%=i %>"> Hello World! </font>
<% } %>

Visual Basic
<% Dim I as Integer
   For I=0 to 9 %>
     <font size="<%=i%>"> Hello World! </font>
<% Next %>
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker