Click here to Skip to main content
14,636,351 members
Articles » Web Development » ASP.NET » General
Posted 14 May 2012

Tagged as


41 bookmarked

Server side Delimiters in ASP.NET

Rate this:
4.89 (43 votes)
Please Sign up or sign in to vote.
4.89 (43 votes)
18 Jun 2012CPOL
This is to see all server side delimiters available for use in ASP.NET


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.


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.
<%@ 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. 
<% { 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. 
<script runat="server"> 
  protected String GetTime()  
     return DateTime.Now.ToString("t");  
Call to above code-behind method from HTML:
<form id="form1" runat="server">
   Current server time: <% =GetTime()%>
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.
<div class="someContent">
   <%: Model.Content %>
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 %>  
<%# ( customer.FirstName + "," + customer.LastName ) %> 
Full detail can be found here: MSDN - Data-Binding Expression Syntax[^
  •  <%$ %> - Expression 
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 %> 
<asp:SqlDataSource ID="SqlDataSource1" Runat="server" 

     SelectCommand="SELECT * FROM [Employees]" 

     ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString1 %>">  
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.


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


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


    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.


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  


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sandeep Mewara
Software Developer (Senior) Bangalore, India
India India
Home Page: My Website
GitHub: SaMewara

Comments and Discussions

GeneralRe: My vote of 5 Pin
Sandeep Mewara22-May-12 20:55
mentorSandeep Mewara22-May-12 20:55 
QuestionInformative and useful Pin
Sergey Alexandrovich Kryukov17-May-12 10:30
MemberSergey Alexandrovich Kryukov17-May-12 10:30 
AnswerRe: Informative and useful Pin
Sandeep Mewara22-May-12 20:56
mentorSandeep Mewara22-May-12 20:56 
GeneralMy vote of 5 Pin
OriginalGriff16-May-12 5:55
mveOriginalGriff16-May-12 5:55 
GeneralRe: My vote of 5 Pin
Sandeep Mewara16-May-12 6:29
mentorSandeep Mewara16-May-12 6:29 
GeneralMy vote of 5 Pin
AspDotNetDev15-May-12 22:47
protectorAspDotNetDev15-May-12 22:47 
GeneralRe: My vote of 5 Pin
Sandeep Mewara16-May-12 1:02
mentorSandeep Mewara16-May-12 1:02 
GeneralMy vote of 5 Pin
VJ Reddy15-May-12 7:08
MemberVJ Reddy15-May-12 7:08 
The content and presentation is very good.
GeneralRe: My vote of 5 Pin
Sandeep Mewara15-May-12 7:20
mentorSandeep Mewara15-May-12 7:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.