4

Is there any difference between performing binding on .aspx page via <%# some code %> and <%= some code %>?

Example:

VS

Thanks. -Igor

  • +close, duplicate: http://stackoverflow.com/questions/370201/why-will-expressions-as-property-values-on-a-server-controls-lead-to-a-com – Juliet Aug 14 '09 at 18:04

2 Answers2

6

<%# %> is used in binding expressions. Simply, when a Control.DataBind is called, the binding expressions will take their actual values. It can be used to set some properties on server controls based on the run time value of an expression.

<%= expression %> is equivalent to <% Response.Write(expression); %> which runs on render phase and directly outputs the value of the expression. As a result, it can't be used to modify behavior of server-side objects.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • and the binding expression will only work on the page level if Page.DataBind() is called. – John Sheehan Aug 14 '09 at 18:02
  • Well, `Page` inherits from `Control`, so that applies to it as a whole too. `DataBind` can be called on individual controls too. – Mehrdad Afshari Aug 14 '09 at 18:04
  • Right. I think people are commonly confused when the put <%# expressions on a page and wonder why nothing shows up. For less experienced devs, its not immediately obvious that Page : Control and that it can have DataBind() called against it. – John Sheehan Aug 14 '09 at 18:07
  • Thanks a lot, all. Wish this kind of stuff would be better documented. –  Aug 14 '09 at 18:32
1

<%= %> is the equivalent of Response.Write();

<%# %> is for databinding when calling .DataBind();

p.campbell
  • 98,673
  • 67
  • 256
  • 322