What is the difference between <%= %> syntax and <%# %> in asp.net?
Here is a complete reference for all of these "inline tags," with links to MSDN for each type:
http://naspinski.net/post/inline-aspnet-tags-sorting-them-all-out-(3c25242c-3c253d2c-3c252c-3c252c-etc).aspx
<%= %> is basically used for string while <%# %> is used for data binding.
See following
http://support.microsoft.com/kb/307860
As compared to the <%= version, <%# is treated specially in parameters. It adds a data binding handler and sets the property at data bind time. That's why <%# does work in parameters and why it picks up the value at data bind time.
Related
I just wan to to know why these different begin tags exist, if there are still more and if they can be used interchangeably:
<%# Do.Something() %>
<%= Do.Something() %>
...
The tags do different things. For example <%= %> will write the result of the expression within the tags to the output, while <%# %> will use data-binding expressions to reference data from a data source. A fuller explanation can be found at http://weblogs.asp.net/ahmedmoosa/archive/2010/10/06/embedded-code-and-inline-server-tags.aspx
where we use <%# %> <%= %> <%# %> etc.
what else asp tags can be added in asp.net web pages?
<%%> is short hand for:
<script runat="server">
</script>
Anyting inside the <% and %> is server side code.
The other variants are also shortcuts:
<%#%> is a page directrive
<%=%> is short for Response.Write
<%:%> is short for Response.Write, adding html encoding (introduced with .NET 4.0)
<%#%> is a binding expression
This page is a good reference to all these.
<%# %> - page directives, Register control
<%= %> - for the server code
<%# %> - for the eval kind of function its also for the server code
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
ASP.NET “special” tags
What is the difference between <%# ... %>, <%= ... %> and <%$ ... %>?
I couldn't find anything information about this. It's impossible to find "<%=" using a search engine.
Do these tags have a name?
<%= ... %> is generally equivalent to Response.Write(...)
it cannot be used in a control attribute that is runat="server"
<%: ... %> (as of .NET v4.0) is an html encoded version of <%= %> (as #Eric mentions)
<%# ... %> is used in data-binding context for Bind, Eval or Output (as #Ray mentions)
<%$ ... %> is used in the context of a control attribute with runat="server" (google "expression builder" also have a look at making a general purpose 'Code' expression builder. it is evaluated when the attribute/Parameter is required by the control.
<%# %> will attempt to databind to a data source, using the Bind() function. This makes it a two-way function (read and write).
<%= %> will make the data read-only.
<%# %> is evaluated during data binding. It does not necessarily require Eval() or Bind() and Matthew suggested - I use it frequently to display plain text in a repeater control.
<%= %> is evaluated as the page renders. It is equivalent to calling Response.Write().
<%# %> can ONLY be used in data-binding context.
<%= %> expects a string value which it will then include in the output stream. So either a string variable or a method which returns a string. Anything else will cause an error.
I found some good information that clarifies the terminology for your future google searches:
http://authors.aspalliance.com/aspxtreme/aspnet/syntax/aspnetpagesyntax.aspx
Code Render Blocks:
<% inline code %>
A shortcut for HttpResponse.Write:
<%=inline expression %>
Data Binding Expressions:
<%# databinding expression %>
In a property:
<tagprefix:tagname property = "<%# databinding expression %>" runat="server" />
Server-side comments, such that they do not appear in the client's page source:
<%-- commented out code or content --%>
This question already has answers here:
Closed 14 years ago.
In ASP.Net, what is the difference between <%= x %> and <%# x %>?
See this question:
When should I use # and = in ASP.NET controls?
Summary from those answers:
There are a several different 'bee-stings':
<%# - Page/Control/Import/Register directive
<%$ - Resource access and Expression building
<%= - Explicit output to page, equivalent to <% Response.Write( ) %>
<%# - Data Binding. It can only used where databinding is supported, or at the page level if you call Page.DataBind() in your code-behind.
<%-- - Server-side comment block
<%: - Equivalent to <%=, but it also html-encodes the output.
<%# is data binding expression syntax.
<%= resolves the expression returns its value to the block (Embedded code block reference) - effectively shorthand for <% Response.Write(...); %>
<%# is the databinding directive, <%= is a shortcut for "Response.Write"
<%= x %> is shorthand for Response.Write()
<%# x %> indicates a databind.
<% %> indicates server-executable code.
What's the difference between <%= Something %> and <%# Something %>?
Are there any other modifiers of this kind?
Duplicate
What is the difference between the <%# and <%= opening tags?
See these questions:
What is the difference between the <%# and <%= opening tags?
In ASP.NET what are the differant ways to inline code in the .aspx?