Home > ASP.Net > ASP.NET Code Delimiters

ASP.NET Code Delimiters

When working in ASP.Net there are a number of code delimiters you need to be aware of, the most recognizable one is <% which, for anyone who has worked with anything from Classic ASP 1.0 (anyone remember IIS 3?) through ASP.Net 4.0 will recognize. Some people call them ASP.net tags or code shortcuts, both of which are only partly true. Calling them “tags” is a little misleading because ASP.NET tags are really things such as “<asp:DropDownList />” and “<asp:ListItem />“. Calling them code shortcuts is a bit misleading as well because, while some of them are shortcuts (<%= ... %> is a shortcut for <% Response.Write (...) %>) not all of them have this kind of purpose.

While “code delimiter” sometimes refers explicitly to the left carat + percent sign and percent sign + right carat only, the verbiage used in Microsoft documentation is usually “code delimiter” and as such is what I am going to refer to them throughout this post. There are a number of different code delimiters available to an ASP.Net developer, but a beginner might have a bit of trouble finding out what they all do. Microsoft lists the majority of them in the ASP.Net Page Syntax section of MSDN (http://msdn.microsoft.com/en-us/library/fy30at8h.aspx) but this post is meant to be a quick access list of those delimiters. While an experienced developer is going to be intimately familiar with all of these, I thought it would be useful to write a short post to list all of them.

One thing to note about the examples I am using below: they are only intended to show how these would look in a block of code, rather than demonstrate real-world usage. Refer to the MSDN article links for more detail on actual usage of each delimiter.

So, without further ado, here is the full list of ASP.NET code delimiters.

<% ... %> – The first code delimiter I will mention is for embedding blocks of inline code (http://msdn.microsoft.com/en-us/library/k6xeyd4z.aspx)

<%
'Basic inline code block
For x = 1 to 100
    Response.Write string.Format("x = {0}<br />", x)
Next
%>
<%= ... %> – The next code delimiter is also for inline code but specifically a shortcut to the inline code block <% Response.Write ( ... ) %>. (http://msdn.microsoft.com/en-us/library/k6xeyd4z.aspx)

Current Date and Time: <%= DateTime.Now().ToString() %>
<%-- ... --%> – Another delimiter that is a shortcut of sorts, perhaps a lesser known one though, is the server-side comment. It is simply used for commenting out information that you don’t want to get sent off to the client. There is a gotcha to the server-side comment however, that you cannot use it in embedded code blocks, which will actually cause a compilation error if you try. (http://msdn.microsoft.com/en-us/library/4acf8afk.aspx)

<%-- You can use a server side comment to keep information from getting to the browser of a user --%>
<%@ ... %> – The template directive is an important delimiter, one that the majority of ASP.Net web forms pages will have on them. There are quite a few different directives that you need to be familiar with, and not all are for web forms pages, but the most common one is probably the page directive, which allows you to specify code behind, master pages, enable or disable viewstate, buffering options, and a plethora of others. (http://msdn.microsoft.com/en-us/library/xz702w3e.aspx)

<%@ Page Title="Home" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" EnableSessionState="False" %>
<%# ... %> – Data-binding expressions are the next important set of code delimiters, which are used to create a binding between a server control property and a data source. (http://msdn.microsoft.com/en-us/library/bda9bbfx.aspx)

<asp:DropDownList ID="ddlInsertUser"
                        SelectedValue='<%# Bind("UserID") %>'
                        DataSourceID="UsersDataSource"
                        DataTextField="UserName"
                        DataValueField="UserID"
                        RunAt="Server" />
<%$ ... %> – The last code delimiter to mention is the ASP.Net Expression delimiter. This delimiter allows you to set the properties of a control to an evaluated expression at runtime. While this has many useful purposes, it is incredibly useful when localizing your web pages. (http://msdn.microsoft.com/en-us/library/d5bd1tad.aspx)

<asp:Label id="label1" runat="server" text="<%$ Resources: LocalizeData, WelcomeLabel %>" />
Categories: ASP.Net Tags:
Share/Save
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.