Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

what really is <%# %>, does Eval, Bind needed?

20 views
Skip to first unread message

Ryan Liu

unread,
Jan 29, 2009, 11:53:27 PM1/29/09
to
Hi,

I found I can use <% # Contaniner.DateItem.Property.SubProperty%> or <%#
page-variable #>

Why or ( when) we need use <%# Evel("Property")%>? And when to use <%# #>
directly?

The first syntax is even more powerful, can access property of propery.

What really is <#% %>? And what does it enclose? C# code or XML code? What
is its relationship with Eval and Bind, do they depend on each other as
syntax requirement?

Thanks,
Ryan


Alexey Smirnov

unread,
Jan 30, 2009, 3:01:18 AM1/30/09
to

Hi Ryan

Array of Strings:
VB/C# <%# Container.DataItem %>

Field from DataView:
VB <%# Container.DataItem("EmployeeName") %>
C# <%# ((DataRowView)Container.DataItem)["EmployeeName"] %>

Property from a collection of objects:
VB <%# Container.DataItem.CustomerName %>
C# <%# ((Customer)Container.DataItem).CustomerName %>

Non-String property from a collection of objects:
VB <%# CStr(Container.DataItem.CustomerID) %>
C# <%# ((Customer)Container.DataItem).CustomerID.ToString() %>

Quoted from: http://weblogs.asp.net/rajbk/archive/2004/07/20/what-s-the-deal-with-databinder-eval-and-container-dataitem.aspx

In ASP.Net 2.0 you can use Eval instead of DataBinder.Eval.

Hope this helps

bruce barker

unread,
Jan 30, 2009, 1:23:38 PM1/30/09
to
<%# %> are binding expressions. they generate code in the data binding
controls like:

ctl.Property = <binding expression>

the expression can be any valid expression that compiles (in your case
it must be valid c#). the scope is the data binding method defined in
the class file generated by the aspx page which is normally a partial
class combined with any codebehind (so in a website you can access
privates defined in the codebehind, but not in a web application).
Container is a reference to the Binding/Naming Container. Eval is a
shortcut for DataBinder.Eval.

Container.DataItem is the object the control is being bound to. its
declared as an object, so to reference any properties (in typed
languages) you need to cast the dataitem to the correct object type or
use refection if the object type is unknown. the Eval method implements
a reflection language to access properties.

-- bruce (sqlwork.com)

0 new messages