Click here to Skip to main content
15,867,453 members
Articles / Web Development / ASP.NET

Server side Delimiters in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.89/5 (43 votes)
18 Jun 2012CPOL5 min read 148.7K   41   62
This is to see all server side delimiters available for use in ASP.NET

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:
ASP.NET
<%@ 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:
HTML
<% { 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:
ASP.NET
<script runat="server"> 
  protected String GetTime()  
  {      
     return DateTime.Now.ToString("t");  
  }  
</script>
Call to above code-behind method from HTML:
ASP.NET
<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: 
HTML
<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.
ASP.NET
<tagprefix:tagname property="<%# data-binding expression %>" runat="server" />  
- or -  
literal text <%# data-binding expression %>  
Sample:
HTML
<%# ( 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.
HTML
<%$ expressionPrefix: expressionValue %> 
Sample:
ASP.NET
<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:

HTML
<%-- Content of comments, or commented out server controls --%>
ASP.NET
<%-- <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   

HTML
<p> Current time is: @DateTime.Now </p>

@{...} For code blocks with multiple lines  

Razor
@{ 
     var name = "John";
     var nameMessage = "Hello, my name is " + name + " Smith";
}

@: For single plain text to be rendered in the page  

Razor
@{ 
    @: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:

HTML
@*
    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  

License

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


Written By
Architect Intuit India
India India


A software professional for more than 17+ years building solutions for Web and Desktop applications.

Currently working at Intuit India.

Website: Learn By Insight
Github: Sandeep Mewara
LinkedIn: Sandeep Mewara

Strongly believe in learning and sharing knowledge.



Comments and Discussions

 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun18-Feb-15 18:57
Humayun Kabir Mamun18-Feb-15 18:57 
GeneralMy vote of 5 Pin
User 1106097920-Jan-15 8:48
User 1106097920-Jan-15 8:48 
Questionusing code nugget Pin
Member 998522712-Oct-14 12:02
Member 998522712-Oct-14 12:02 
GeneralMy vote of 5 Pin
Renju Vinod2-May-13 19:48
professionalRenju Vinod2-May-13 19:48 
QuestionCan I add a variable value inside scriptlet? Pin
Member 1051082219-Feb-13 20:31
professionalMember 1051082219-Feb-13 20:31 
GeneralMy vote of 5 Pin
Sergey Alexandrovich Kryukov15-Feb-13 9:56
mvaSergey Alexandrovich Kryukov15-Feb-13 9:56 
GeneralRe: My vote of 5 Pin
Sandeep Mewara15-Feb-13 10:00
mveSandeep Mewara15-Feb-13 10:00 
GeneralMy vote of 5 Pin
Savalia Manoj M3-Feb-13 16:42
Savalia Manoj M3-Feb-13 16:42 
GeneralRe: My vote of 5 Pin
Sandeep Mewara7-Feb-13 4:34
mveSandeep Mewara7-Feb-13 4:34 
Thanks.
Sandeep Mewara
Microsoft ASP.NET MVP 2012 & 2013

[My Blog]

[My Latest Post]: How to extend a WPF Textbox to Custom Picker

GeneralMy vote of 5 Pin
Ali Al Omairi(Abu AlHassan)20-Jan-13 2:29
professionalAli Al Omairi(Abu AlHassan)20-Jan-13 2:29 
GeneralRe: My vote of 5 Pin
Sandeep Mewara29-Jan-13 7:07
mveSandeep Mewara29-Jan-13 7:07 
GeneralMy vote of 5 Pin
Chandra Mohan BS26-Nov-12 6:13
Chandra Mohan BS26-Nov-12 6:13 
GeneralRe: My vote of 5 Pin
Sandeep Mewara26-Nov-12 16:00
mveSandeep Mewara26-Nov-12 16:00 
GeneralMy vote of 5 Pin
Chandra Mohan BS26-Nov-12 6:09
Chandra Mohan BS26-Nov-12 6:09 
GeneralRe: My vote of 5 Pin
Sandeep Mewara26-Nov-12 16:00
mveSandeep Mewara26-Nov-12 16:00 
GeneralMy vote of 5 Pin
mickodr15-Oct-12 13:55
mickodr15-Oct-12 13:55 
GeneralRe: My vote of 5 Pin
Sandeep Mewara15-Oct-12 18:11
mveSandeep Mewara15-Oct-12 18:11 
AnswerMy vote of 5 Pin
Rahul Rajat Singh14-Oct-12 18:04
professionalRahul Rajat Singh14-Oct-12 18:04 
GeneralRe: My vote of 5 Pin
Sandeep Mewara15-Oct-12 7:40
mveSandeep Mewara15-Oct-12 7:40 
QuestionGood Post Pin
sumB17-Jul-12 0:02
sumB17-Jul-12 0:02 
AnswerRe: Good Post Pin
Sandeep Mewara17-Jul-12 0:32
mveSandeep Mewara17-Jul-12 0:32 
GeneralRe: Good Post Pin
vimalrns3-Sep-12 2:38
professionalvimalrns3-Sep-12 2:38 
GeneralMy vote of 2 Pin
NaveenMupalla4-Jul-12 23:01
NaveenMupalla4-Jul-12 23:01 
QuestionRe: My vote of 2 Pin
Sandeep Mewara17-Jul-12 0:35
mveSandeep Mewara17-Jul-12 0:35 
GeneralMy vote of 1 Pin
Member 107456825-Jun-12 19:01
Member 107456825-Jun-12 19:01 

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.