4

I've tried to perform a search on the above query but probably due to the angle brackets, I didn't find anything on the matter.

Could anyone please explain what the differences are between <%=, <%: <%#?

I seem to recall that <%# is preferred over <%= but I am not sure why.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
DavidS
  • 2,179
  • 4
  • 26
  • 44

2 Answers2

3

<%= xxx %> Inserts the text in xxx into the page at that location. (more info)

<%: xxx %> Same as above except it html encodes the text for your convenience - (Except if xxx is an HtmlString which indicates it is already encoded)

<%# xxx %> Same as the first one too except xxx is only evaluated when DataBind() is called on the form (not really applicable in MVC) (more info)

Martin

jgauffin
  • 99,844
  • 45
  • 235
  • 372
Martin Booth
  • 8,485
  • 31
  • 31
  • Excellent answer which is pretty much what Darin wrote. I marked his reply as the answer because the link he provided taught me something new i.e. <%$ – DavidS Jul 08 '11 at 08:35
3

The following article describes them pretty well.

  • <%=: Rendering Code Syntax
  • <%: %>: HTML encoded renedring (same usage as <%=)
  • <%# %>: Data Binding Syntax - works with server side controls in classic WebForms applications, inapplicable in MVC
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • thank you. I'm a bit surprised about the <%# because I believe that I picked up the latter in an MVC tutorial on the asp.net site. :S – DavidS Jul 08 '11 at 08:37