Reputation: 4602
I have been using ASP.NET for years, but I can never remember when using the # and = are appropriate.
For example:
<%= Grid.ClientID %>
or
<%# Eval("FullName")%>
Can someone explain when each should be used so I can keep it straight in my mind? Is # only used in controls that support databinding?
Upvotes: 27
Views: 4827
Reputation: 155892
There are a couple of different 'bee-stings':
<%@
- page directive<%$
- resource access<%=
- explicit output to page<%#
- data binding<%--
- server side comment blockAlso new in ASP.Net 4:
<%:
- writes out to the page, but with HTML encodedAlso new in ASP.Net 4.5:
<%#:
- HTML encoded data bindingUpvotes: 44
Reputation: 21873
Here's a great blog post by Dan Crevier that walks through a test app he wrote to show the differences.
In essence:
Upvotes: 9
Reputation: 78152
<%= %> is the equivalent of doing Response.Write("") wherever you place it.
<%# %> is for Databinding and can only be used where databinding is supported (you can use these on the page-level outside a control if you call Page.DataBind() in your codebehind)
Databinding Expressions Overview
Upvotes: 24