tags:

views:

326

answers:

6

Pardon my ASP ignorance, but what's the difference?

+2  A: 

The # version is used while data binding. <%= is just a simple Response.Write

AlbertEin
+3  A: 

See http://weblogs.asp.net/leftslipper/archive/2007/06/29/how-asp-net-databinding-deals-with-eval-and-bind-statements.aspx

As Albert says, it's all to do with parsing databinding statements.

Oli
+13  A: 

These are somewhat informally referred to as "bee stings". There are 4 types:

<%# %> is invoked during the DataBinding phase.

<%= %> is used to get values from code to the UI layer. Meant for backward compatibility with ASP applications. Shouldn't use in .NET.

<%@ %> represents directives and allow behaviors to be set without resorting to code.

<%: %> (introduced in ASP.NET 4) is the same as %=, but with the added functionality of HtmlEncoding the output. The intention is for this to be the default usage (over %=) to help shield against script injection attacks.

Directives specify settings that are used by the page and user-control compilers when the compilers process ASP.NET Web Forms pages (.aspx files) and user control (.ascx) files.

ASP.NET treats any directive block (<%@ %>) that does not contain an explicit directive name as an @ Page directive (for a page) or as an @ Control directive (for a user control).

@Esteban - Added a msdn link to directives. If you need...more explanation, please let me know.

Swati
Swait, could you please expound on <%@ %>. I'm not sure I understand what you mean. Thanks!
Esteban Araya
Take a peek at any .NET page you have with AJAX or Master Pages. You'll find the <%@ $> directive in there to indicate what pages/libraries should be loaded.
Dillie-O
<%= %> is perfectly valid for ASP.NET applications. In fact, it's the only way to get values from code to the UI layer with ASP.NET MVC. If you're using ASP.NET web forms then yes, it is discouraged, but still useful for injecting server side values into javascript.
Kevin Pang
I agree with Kevin, <%= %> is OK for ASP.NET MVC but not for ASP.NET Web Forms.
Sayed Ibrahim Hashimi
+2  A: 

Not entirely related to the question, there's another related notation in asp.net called Expression Builder:

<asp:SqlDataSource ... Runat="server"
 ConnectionString="<%$ ConnectionStrings:Northwind %>"
/>

<asp:Literal Runat="server"
  Text="<%$ Resources:MyResources, MyText %>"
/>

and it's extensible, see http://msdn.microsoft.com/en-us/magazine/cc163849.aspx#S4

wangzq
A: 

javascript in .aspx that uses a master page.

var e = document.getElementById('<%= lblDescription.ClientID %>');
e.innerHTML = 'getElementById(\'lblDescription\') will be null';
mangokun