Introduction
Recently, while browsing Question & Answer section, I came across a question that asked for various scripting delimiters and their usage. It took some time for me to find appropriate links related to each of them to back up my answer. So, I thought, why not post all of them in one place as a Tip/Trick here.
Background
A server-side script is a series of instructions used to sequentially issue commands to the Web server. These scripts are differentiated from text and HTML by delimiters. A delimiter is a character or sequence of characters that marks the beginning or end of a unit. ASP.NET (and ASP too) uses the delimiters <%
and %>
to enclose script commands. Within the delimiters, you can include any command that is valid for the scripting language you are using.
Using the code
<%@ %>
- Text Template Directive
This specifies settings used by the page and user control compilers when they process ASP.NET Web Forms page (.aspx) and user control (.ascx) files.
Sample:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
The ASP.NET page framework supports quite a few directives - @Page
, @Control
, @Import
, @Implements
, @Register
, @Assembly
, @Master
, @WebHandler
, @PreviousPageType
, @MasterType
, @OutputCache
, @Reference
Full detail can be found here: MSDN - Text Template Directive Syntax[^]
<%= %>
& <% %>
- Embedded Code Blocks
An embedded code block is server code that executes during the page's render phase. The code in the block can execute programming statements and call functions in the current page class.
Sample:
<% { Response.Write("Hello World!!!"; }%>
In embedded code blocks, the syntax <% = expression %> is used to resolve an expression and return its value into the block. Embedded code blocks must be written in the page's default language. Embedded code blocks are supported in ASP.NET Web pages primarily to preserve backward compatibility with older ASP technology. At times, this embedded code blocks help in interacting with server side at runtime.
Sample:
<script runat="server">
protected String GetTime()
{
return DateTime.Now.ToString("t");
}
</script>
Call to above code-behind method from HTML:
<form id="form1" runat="server">
Current server time: <% =GetTime()%>
</form>
Full detail can be found here: MSDN - Embedded Code Blocks in ASP.NET Web Pages[^]
-
<%: %>
- HTML Encoding Output
This is to automatically HTML encode output given within the code nuggets. This helps protect your applications and sites against cross-site script injection (XSS) and HTML injection attacks, and enables you to do so using a nice concise syntax.
ASP.NET applications (especially those using ASP.NET MVC) often rely on using <%= %>
code-nugget expressions to render output. With ASP.NET 4, a new code expression syntax <%: %>
is defined that renders output like <%= %>
blocks do, but which also automatically HTML encodes it before doing so. This eliminates the need to explicitly HTML encode content.
Sample:
<div class="someContent">
<%: Model.Content %>
</div>
Full detail can be found here: ScottGu Blogpost - New Syntax for HTML Encoding Output in ASP.NET[^]
<%# %>
- Data-binding Expression
This syntax is the basis for using data binding in an .aspx page. All data binding expressions must be contained within these characters.
Data-binding expressions create bindings between a server control property and a data source when the DataBind method is called on the page. You can include data-binding expressions on the value side of an attribute/value pair in the opening tag of a server control, or anywhere in the page.
<tagprefix:tagname property="<%# data-binding expression %>" runat="server" />
- or -
literal text <%# data-binding expression %>
Sample:
<%# ( customer.FirstName + "," + customer.LastName ) %>
Full detail can be found here: MSDN - Data-Binding Expression Syntax[^]
They are a declarative way set control properties based on information that is evaluated at run time. A common use of expressions is in data source controls to reference a connection string, appsettings or resources.
The dollar sign $
indicates to ASP.NET that an expression follows. The expression prefix defines the type of expression, such as AppSettings, ConnectionStrings, or Resources. Following the colon (:
) is the actual expression value that ASP.NET will resolve.
<%$ expressionPrefix: expressionValue %>
Sample:
<asp:SqlDataSource ID="SqlDataSource1" Runat="server"
SelectCommand="SELECT * FROM [Employees]"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString1 %>">
</asp:SqlDataSource>
Full detail can be found here: MSDN - ASP.NET Expressions Overview[^]
<%-- --%>
- Server Side Comments
These allow developers to embed code comments in any part of an ASP.NET application file. Any content between opening and closing tags of server-side comment elements, whether ASP.NET code or literal text, will not be processed on the server or rendered to the resulting page.
ASP.NET server-side comment blocks have the same uses as traditional language-specific comment blocks, including documentation and testing.
Sample:
<%-- Content of comments, or commented out server controls --%>
<%-- <asp:button runat="server" id="MyButton" OnClick="MyButton_Click" /> --%>
Full detail can be found here: MSDN - Server-Side Comments[^]
@
- Razor Code instructions
@ is a character that precedes code instructions in MVC Razor.
Even to write HTML inside a page, there is no need to open or close code blocks.
If you want to add a code instruction inside HTML, you will need to use
‘@’ before the code.
Sample:
@
For a single code line/values
<p> Current time is: @DateTime.Now </p>
@{...}
For code blocks with multiple lines
@{
var name = “John”;
var nameMessage = "Hello, my name is " + name + " Smith";
}
@:
For single plain text to be rendered in the page
@{
@:The day is: @DateTime.Now.DayOfWeek. It is a <b>great</b> day!
}
Full detail can be found here: MSDN - Razor Syntax – The fundamentals[^]
@* *@
- Comments in Razor
Comments in Razor are delimited by @* *@, similar to C# code block, where we use // and /* */ comment delimiters.
Sample:
@*
A Razor Comment
*@
@{
//A C# comment
/* A Multi
line C# comment
*/
}
Full detail can be found here: MSDN - Razor Syntax – The fundamentals[^]
That would sum up all the delimiters.
Points of Interest
Well, I did learn how couple of them differ from each other. Finding about all of them at one place was tough - hope this tip keeps things clear and concise.
History
Version 3 - 15 June 2012 - Moved it from Tip to article section as a reference article
Version 2 - 10 June 2012 - (Thanks to Pete O'Hanlon for providing details[^] for this update.)
Version 1 - 14 May 2012