<%# tags

  • Thread starter Thread starter Steve Bywaters
  • Start date Start date
S

Steve Bywaters

Are the <%..%> and <%#..%> tags *both* used in ASP.NET?
Is there a difference?

Steve
 
Steve,

The <%# %> tags are used for databinding, while <%= %> are more general. The
<%# %> tend to be used to be in scope with the current databinding context -
for example the current row in a datagrid as it renders which couldn't be
addressed with plain <%= %> syntax.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
 
In <% you can put any code in there, dim variables, whatnot.

In <%= that's actually short for response.write, so you are limited to an
expression.

In <%# this is a databinding expression used inside the scope of a web
control (such as <asp:Repeater ) where you have a Container to use to access
the items you've bound to that particular control. What you can expose is a
DataItem -- which are your value pairs. ie <%#
DataBinder.Eval(Container.DataItem, "Name") %>

Like <%= you must supply an expression, you cannot write any code in these
blocks. If you don't use the Container class, then it's the same as saying
<%= only you're using up more resources to create the Container you didn't
use.

And of course, if you need some more code in this block, just simply write a
function that returns exactly the string you want to show:

<%# GetMyText(Container.DataItem, "Name") %>

Unfortunately, using the <%# is a late bound operation, so you're getting
ASP like performance from an ASP.NET application. Not a big deal normally,
it's still superfast, but this would be the place to look if you ever start
having performance issues.

-Max
 
Back
Top