226

I've programmed in both classic ASP and ASP.NET, and I see different tags inside of the markup for server side code.

I've recently come across a good blog on MSDN that goes over the difference between:

  • <%= (percentage together with equals sign) and
  • <%# (percent sign and hash/pound/octothorpe)

(<%# is evaluated only at databind, and <%= is evaluated at render), but I also see:

  • <%$ (percent and dollar sign) and
  • <%@ (percent sign and at symbol).

I believe <%@ loads things like assemblies and perhaps <%$ loads things from config files? I'm not too sure.

I was just wondering if anyone could clarify all of this for me and possibly explain why it's important to create so many different tags that seemingly have a similar purpose?

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
Aaron
  • 7,431
  • 12
  • 35
  • 37
  • 77
    I'm glad you ask, because it's very hard to google ;) – Nicolas Dorier Jun 05 '09 at 18:18
  • 10
    Impossible to Google! – jdbosley Jul 25 '14 at 16:18
  • 1
    `<%#` is often used with `eval`, but it doesn't have to: you can use it to run any server side code, provided that you run `Page.DataBind()` on the containing page or its master page. It seems that this is your only option in case you want to "inject" server side code into attributes of server side controls such as ``. – Gilad Barner Apr 30 '15 at 07:20
  • Its not hard to google. I had used greater than Percentage equal to in aspx to come on this page...Try it , Bit tricky but works fine ;) – Mayank Pathak May 18 '15 at 06:53
  • 1
    @jdbosley I googled `"<%=" "<%:""<%@" "<%#"` and it sent me here. – Rudey Apr 04 '17 at 08:57
  • 2
    @RuudLenders, lucky for you and good job Google. In 2014 searching those tags did not return good quality results. – jdbosley Jun 06 '17 at 20:57

2 Answers2

319
Maris B.
  • 2,333
  • 3
  • 21
  • 35
Jose Basilio
  • 50,714
  • 13
  • 121
  • 117
  • I guess it's because it's an accepted answer, but trying to edit /w the link didn't work. here's the link I tried putting in about the "<%$" expression evaluation: http://msdn.microsoft.com/en-us/library/d5bd1tad.aspx – John MacIntyre Sep 15 '11 at 21:32
  • Awesome! I know this is old but I have been looking all over the internet for this info the last 2 days. Thanks! – jdbosley Jul 25 '14 at 16:16
  • 8
    Is there a name for all these? What are these called as a group? – Vippy Feb 26 '15 at 18:25
  • 11
    @Vippy They are called *bee stings*. – Ian Boyd Jun 30 '15 at 18:46
  • 1
    @IanBoyd Where does the term *bee stings* come from? – user692942 Mar 15 '16 at 13:27
  • 2
    @Lankymart Earliest usage i can find is [September 2008](http://stackoverflow.com/a/115212/12597) – Ian Boyd Mar 15 '16 at 19:39
  • 1
    Maybe my testing method sucks, but I can't see a difference between <%# %> and <%#: %>. Both of them HTML encode the value. – Dan Jameson May 23 '16 at 03:39
  • 1
    @DanJameson Many ASP.Net controls (e.g. TextBox) automatically HTML Encode their contents. If you try outputting a value directly to the page, or even a Label, you'll see the difference between `<%# #>` and `<%#: #>`. – Benjamin Ray Feb 28 '17 at 14:47
  • 1
    @Vippy MS calls them [Embedded code blocks](https://msdn.microsoft.com/en-us/library/ms178135.aspx) "Bee stings" is an unofficial term. – shiggity Apr 25 '17 at 14:01
6

You've covered 2 of them (<%# is evaluated only at databind, and <%= is evaluated at render), and the answer for "<%@" is that it's compiler directives (ie., stuff like what you'd put on a compiler's command line).

I don't know about "<%$".

Michael Burr
  • 333,147
  • 50
  • 533
  • 760