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”) %>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.