Rendering Code Syntax: <% %> and <%= %>
Code rendering blocks are denoted with <% ... %> elements, allow you to custom-control
content emission, and execute during the render phase of Web Forms page execution.
The following example demonstrates how you can use them to loop over HTML content.
<% For I=0 To 7 %>
<font size="<%=i%>"> Hello World! </font> <br>
<% Next %>
VB
Code enclosed by <% ... %> is just executed, while expressions that include an
equal sign, <%= ... %>, are evaluated and the result is emitted as content.
Therefore <%="Hello World" %> renders the same thing as the C#
code <% Response.Write("Hello World"); %>.
Note: For languages that utilize marks to end or separate statements
(for example, the semicolon (;) in C#), it is important to place those marks correctly depending on how your code should be rendered.
| C# code |
<% Response.Write("Hello World"); %> |
A semicolon is necessary to end the statement. |
<%="Hello World"; %> |
Wrong: Would result in "Response.Write("Hello World";);". |
<%="Hello World" %> | A semicolon is not necessary. |