Click here to Skip to main content
Click here to Skip to main content

Server side Delimiters in ASP.NET

, 18 Jun 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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:
<%@ 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[^
  •  <%$ %> - 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 %> 
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  

License

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

Share

About the Author

Sandeep Mewara
Software Developer (Senior)
India India
Current location: Bangalore, India.
Follow on   Twitter

Comments and Discussions

 
Questionusing code nugget PinmemberMember 998522712-Oct-14 13:02 
GeneralMy vote of 5 PinprofessionalRenju Vinod2-May-13 20:48 
QuestionCan I add a variable value inside scriptlet? PinmemberMd. Humayun Rashed19-Feb-13 21:31 
GeneralMy vote of 5 PinmvpSergey Alexandrovich Kryukov15-Feb-13 10:56 
GeneralRe: My vote of 5 PinmvpSandeep Mewara15-Feb-13 11:00 
QuestionMessage Automatically Removed Pinmemberetechpulse7-Feb-13 2:25 
GeneralMy vote of 5 PinmemberSavalia Manoj M3-Feb-13 17:42 
GeneralRe: My vote of 5 PinmvpSandeep Mewara7-Feb-13 5:34 
GeneralMy vote of 5 PinmemberAli Al Omairi(Abu AlHassan)20-Jan-13 3:29 
GeneralRe: My vote of 5 PinmvpSandeep Mewara29-Jan-13 8:07 
GeneralMy vote of 5 PinmemberChandra Mohan26-Nov-12 7:13 
GeneralRe: My vote of 5 PinmvpSandeep Mewara26-Nov-12 17:00 
GeneralMy vote of 5 PinmemberChandra Mohan26-Nov-12 7:09 
GeneralRe: My vote of 5 PinmvpSandeep Mewara26-Nov-12 17:00 
GeneralMy vote of 5 Pinmembermickodr15-Oct-12 14:55 
GeneralRe: My vote of 5 PinmvpSandeep Mewara15-Oct-12 19:11 
AnswerMy vote of 5 PinmemberRahul Rajat Singh14-Oct-12 19:04 
GeneralRe: My vote of 5 PinmvpSandeep Mewara15-Oct-12 8:40 
QuestionGood Post PinmembersumB17-Jul-12 1:02 
AnswerRe: Good Post PinmvpSandeep Mewara17-Jul-12 1:32 
GeneralRe: Good Post Pinmembervimalrns3-Sep-12 3:38 
hi
GeneralMy vote of 2 PinmemberNaveenMupalla5-Jul-12 0:01 
QuestionRe: My vote of 2 PinmvpSandeep Mewara17-Jul-12 1:35 
GeneralMy vote of 1 PinmemberMember 107456825-Jun-12 20:01 
GeneralRe: My vote of 1 PinmvpSandeep Mewara17-Jul-12 1:36 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 18 Jun 2012
Article Copyright 2012 by Sandeep Mewara
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid