views:

84

answers:

2

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

Example:

<%# ResolveUrl("~/default.aspx") %>

VS

<%= ResolveUrl("~/default.aspx") %>

Thanks. -Igor

+1  A: 

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

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

p.campbell
+6  A: 

<%# %> 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
and the binding expression will only work on the page level if Page.DataBind() is called.
John Sheehan
Well, `Page` inherits from `Control`, so that applies to it as a whole too. `DataBind` can be called on individual controls too.
Mehrdad Afshari
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
Thanks a lot, all. Wish this kind of stuff would be better documented.
Igor