27

I have been using ASP.NET for years, but I can never remember when using the # and = are appropriate.

For example:

<%= Grid.ClientID %>

or

<%# Eval("FullName")%>

Can someone explain when each should be used so I can keep it straight in my mind? Is # only used in controls that support databinding?

John Sheehan
  • 77,456
  • 30
  • 160
  • 194
y0mbo
  • 4,582
  • 6
  • 41
  • 45
  • Related (duplicate?): [In ASP.NET, what are the different ways to inline code in the .aspx?](http://stackoverflow.com/questions/28219). – Peter Mortensen Mar 30 '11 at 09:56
  • I've didn't check the theory behind this, but <%# seems to be evaluated BEFORE <%=, so even if accessed variable may exist and is set during lifetime, it may return null. I've just lost a few time to fix a such issue (very tricky situation in my case), so I thought it could be useful to share it. – AFract Sep 25 '14 at 12:39

3 Answers3

44

There are a couple of different 'bee-stings':

  • <%@ - page directive
  • <%$ - resource access
  • <%= - explicit output to page
  • <%# - data binding
  • <%-- - server side comment block

Also new in ASP.Net 4:

  • <%: - writes out to the page, but with HTML encoded

Also new in ASP.Net 4.5:

  • <%#: - HTML encoded data binding
Brian
  • 25,523
  • 18
  • 82
  • 173
Keith
  • 150,284
  • 78
  • 298
  • 434
  • 4
    <%$ is not just for resource access, but for ExpressionBuilders - of which ConnectionStrings, AppSettings, and Resource are included in ASP.NET. It's also trivial to write your own. http://msdn.microsoft.com/en-us/library/system.web.compilation.expressionbuilder.aspx – Mark Brackett Nov 24 '08 at 16:53
  • 1
    Quite a nice explanation here: http://michielvoo.net/blog/expressions-vs-statements-part-2-asp-net-code-block-types/ – Keith Jun 08 '09 at 13:26
  • 2
    Where does the phrase *bee-stings* come from? – user692942 Mar 15 '16 at 19:51
  • 3
    @Lankymart in the default Visual Studio colour schemes these were highlighted in black and yellow, so `%>` looked a little like a bee sting. – Keith Mar 15 '16 at 19:55
  • 1
    Thanks, I was just intrigued where the phrase came from in relation to ASP code blocks, I've never heard before. – user692942 Mar 15 '16 at 19:57
  • Looks like link you posted died back in 2013, but can still be accessed here [Expressions vs. Statements, part 2: ASP.NET Code Block Types via Archive.org](http://web.archive.org/web/20130203221323/http://michielvoo.net/blog/expressions-vs-statements-part-2-asp-net-code-block-types/) – user692942 Mar 15 '16 at 20:03
24

<%= %> is the equivalent of doing Response.Write("") wherever you place it.

<%# %> is for Databinding and can only be used where databinding is supported (you can use these on the page-level outside a control if you call Page.DataBind() in your codebehind)

Databinding Expressions Overview

John Sheehan
  • 77,456
  • 30
  • 160
  • 194
  • see Gui Starbuck answer https://stackoverflow.com/questions/115159/when-should-i-use-and-in-asp-net-controls/115205#115205 for the differences between <%= and <%# – Max Feb 05 '19 at 07:52
9

Here's a great blog post by Dan Crevier that walks through a test app he wrote to show the differences.

In essence:

  • The <%= expressions are evaluated at render time
  • The <%# expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.
  • <%# expressions can be used as properties in server-side controls. <%= expressions cannot.
Guy Starbuck
  • 21,603
  • 7
  • 53
  • 64
  • "<%= expressions cannot." Because <%= %> is a shortcut for Response.Write which happens _*after*_ the page is rendered and the response is being streamed back to the browse. – AMissico Dec 24 '09 at 18:01
  • These are the real differences – Max Feb 05 '19 at 07:50