Code blocks in asp.net

There are really four types of code blocks, and the first one is different from the others:

  •  <%$ %>
  •  <%# %>
  •  <% %>
  •  <%= %>

ASP.NET Expression Syntax
First of all we have ASP.NET expressions which look like <%$ AppSettings:Key %>
<asp:Label runat=”server” Text=”<%$ AppSettings:Key %>” />

ASP.NET Data-Binding syntax
The next code construct is the data-binding syntax: <%# Eval(“Value”) %> which is used to bind to properties to data on demand.

Statement Block

Display some values
<%
string message = “Hello World!”;
Response.Write(message);
%>

Expression/Evaluated Code Blocks

These are delimited by <%= and %> and the content of this code block becomes the parameter to the
HtmlTextWrite.Write() method. Therefore, the code inside this type of code block should be an
expression, and not a statement.
<%= String.Format(“The title of this page is: {0}”, this.Title ?? “n/a”) %>