249

What is the official name for the "special" ASP.NET tags like this:

<%# %>
<%= %>
<%@ %>
<%$ %>

I can't seem to figure out the conceptual or well known name for these, so I'm having trouble searching for more info. As a bonus, can anyone give me a quick rundown of all of the possible "special tags" and what each one of them does (or point me to a resource)?

1

5 Answers 5

381

The official name is "server-side scripting delimiters" or "ASP.NET inline expressions". Visual Studio 2008 syntax highlighting settings dialog calls these "HTML Server-Side Script". Microsoft guys call them "code nuggets" in their blogs.

  • <%@ %> is a Directive for ASP.NET Web Pages. Used for pages and controls to configure page/control compiler settings (<%@ Control Inherits="MyParentControl" %>).
    • <%@ %> is also an Application Directive. Used to specify application-specific settings for global.asax. Distinct from the page directives as it only uses a different tag set.
  • <% %> is a Code Render Block (for inline code). One of 4 forms of Embedded Code Blocks. Used for inclusion of server-side code to the Render() method (<% x = x + 1; %>) of the generated class. Format: single/multiline or multiple-linked (e.g. if/then/else interspersed with html) but cannot be used to declare functions.
  • <%= %> is a Code Render Block (for inline expressions). Used as a shorthand for <%Response.Write(value)%>
  • <%: %> (unofficially an "Html Encoding Code Block") is the same as previous, but the output is HTML encoded.
  • <%# %> is a Data-binding Expression. Used for one-way (readonly) or two-way (updateable) binding through Eval, Xpath, Bind, or expressions (e.g. the selected value of a drop-down control). Binds expressions to data-bound control properties through the control's attribute markup, or as a separate tag which generates a DataBoundLiteralControl instance with the value on its Text property. Expressions are evaluated by a DataBinding event handler for the control.
  • <%#: %> is an HTML Encoded Data-Binding Expression (new in ASP.NET 4.5). It combines the functionality of <%# %> and <%: %>.
  • <%$ %> is an ASP.NET Expression Builder. Used for runtime expression binding for control properties through the server tag attributes. Used with AppSettings, ConnectionStrings, or Resources (or your own custom extension, for example to use code-behind properties). These assignments are added to the OnInit() method of the generated class.
  • <%-- --%> is a Server-Side Comment. Used to exclude the contents from compilation (and so will generate errors if a commented-out control is referred to in code-behind). Unlike html comments the contents will not be included in the output.
  • <!-- #Include ... --> is a Server-Side Include Directive. Used to insert the contents of a file into the page, control or global file. Useful where a user control is overkill, and a master page cannot be used.

There is also a Code Declaration Block, the final Embedded Code Block form.

<script runat="server">
bool IsTrue() {
  return false;
}
</script>

This is used to include additional members (methods etc.) to the class generated from the ASP.NET markup. These have only ever been provided "primarily to preserve backward compatibility with older ASP technology" and are not recommended for use.

3
7

No answer for your name question, but the MSDN "ASP.NET Page Syntax" page is pretty good (or rather, that's the top level page; the pages under it give more information).

EDIT: I had previously thought that <%# ... %> wasn't included in the list, but of course it is, under Data-Binding Expressions. Doh.

3
  • This link currently doesn't work. You have to select a .NET framework version before it does.
    – Sam
    Commented Mar 13, 2013 at 2:44
  • 1
    @Sam: Ah, thanks - I think it was retired when .NET 4.5 came out, for some reason. I've edited the link.
    – Jon Skeet
    Commented Mar 13, 2013 at 6:52
  • 1
    I thought Jon Skeet wasn't doing web development...!
    – CodeArtist
    Commented Nov 13, 2014 at 4:28
6

I have always found this QuickStart page to be very useful, whenever I have a doubt about Server-side syntax. It details 8 different markup styles and provides illustrative examples of each one, in addition to explaining the pros and cons.

It doesn't mention the Page level directive, though, which IIRC is detailed elsewhere in the Quickstart.

Of course, this is relevant to ASP.NET 2.0.

3
6

Microsoft guys call them "nuggets" or "code nuggets" sometimes.

0

I've also heard these called "V stings," including on the StackOverflow Podcast.

Not the answer you're looking for? Browse other questions tagged or ask your own question.