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

  • Thread starter Thread starter Ryan Liu
  • Start date Start date
R

Ryan Liu

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
 
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

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/archiv...h-databinder-eval-and-container-dataitem.aspx

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

Hope this helps
 
<%# %> 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)
 
Back
Top