views:

115

answers:

2

Hi,

Can you briefly list the differences between <%= %>, <%# %> and <%$ %> by giving a simple example maybe that requires only one of those expressions to be used?

Thanks

+17  A: 

<% %>

<% this.CallMethod() %> - Basic code block that executes the statements inside.


<%= %>

<%= "text" %> - Embedded code syntax. Same as writing <% Response.Write("text") %>.


<%: %>

<%: "text" %> - Same as above except it's a shorthand for <%= Server.HtmlEncode("text") %>. This was introduced in ASP.NET 4 and is the default syntax used.


<%# %>

<%# Eval("ColumnName") %> - Used for databinding.


<%$ %>

<%$ AppSettings: settingName %> - The expression syntax has a prefix such as AppSettings, ConnectionStrings, or Resources and then a : followed by the actual expression. It can be used as a shorthand to access resources inline. You can even create your own syntax used here (Thanks @Thomas Levesque). Also see MSDN for more info.


<%@ %>

<%@ Page language="C#" %> - The directive syntax useful for page/control settings.


<%-- --%>

<%-- This is a comment --%> - Server-side comment syntax. This differs from the HTML <!-- a comment --> syntax in that it won't be rendered in the output.

TheCloudlessSky
It's interesting to note that you can create your own expression syntax extensions for use with `<%$ %>`, although it isn't very well documented
Thomas Levesque
@Thomas - I found a great article and MSDN info about the custom expressions (updated my post). You've opened a whole new world!
TheCloudlessSky
Tank you very much guys!
burak ozdogan
A: 

Somebody asked this earlier. Excellent response:

http://stackoverflow.com/questions/115159

asp316