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

DataBinder.Eval performance penalty?

34 views
Skip to first unread message

Beginner

unread,
Mar 6, 2002, 5:46:26 PM3/6/02
to
According to Microsoft .NET QucikStart

" It is important to note that DataBinder.Eval can carry a noticeable
performance penalty over the standard data binding syntax because it uses
late-bound reflection. Use DataBinder.Eval judiciously, especially when
string formatting is not required. "

I'm mainly using Repeater, is there a "Standard Data Binding Syntax" I can
use in the example below?

<ItemTemplate><tr><td><%# DataBinder.Eval(Container.DataItem, "username")
%></td></tr></ItemTemplate>

Jim Ross [MVP]

unread,
Mar 6, 2002, 6:39:12 PM3/6/02
to
If you don't need formatting, such as for a date, I have found that if

DataBinder.Eval(Container.DataItem, "username")
works, then so will
Container.DataItem("username")

"Beginner" <bzh...@hotmail.com> wrote:

Jim Ross
MS MVP [VC/MFC]

To send email, change 'lotsofspamthroughhere' to 'msn' but please ask all questions in the newsgroups, not via private mail

Daniel Naumann

unread,
Mar 6, 2002, 8:01:00 PM3/6/02
to
Here is a copy of an extremely good explanation of the Data Binding
situation your in. it should help explain why there is a performance
hit.............

---------- Begin --------------

>From the wonderful Susan Warren on 11/30/2000 1:48:00 PM:

The databinding expression <%# some expression %> is evaluated in the
language of the page (VB, C#, etc.) This can have a big impact on the
corrent syntax, so be very careful you are looking at docs for the same
language you are using.


Container.DataItem is a runtime alias for the DataItem for this specific
item in the bound list. For a grid which displays 10 rows of data, this is
one row from the datasource. The actual type of DataItem depends is
determined by the type of the datasource. For example, if the datasource is
a Dataview, the type of DataItem is DataRowView. If the type of the
datasource is an array of strings, the type of DataItem is String. If the
datasource is a collection of strongly-typed objects (for example
"Employees" objects), the type of DataItem is Employees.


Each of these cases requires a little different databinding expression, with
further differences between VB and C#. In every case, you want the
databinding expression to produce a *string* that can be displayed in the
page.

Here are some examples:

Array of strings:
<%# Container.DataItem %> (same for VB and C#)

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


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


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


As you can see the syntax is tricky, especially for C#, which requires
explicit casting. So we've provided a "DataBinder.Eval()" helper method
that figures out the syntax for you *and* formats the result as a string.
It's really convenient, with a couple of caveats: it's late bound (uses
reflection at runtime to figure out the data types), and it only
supports basic data types in the fields: string, int, datetime, etc.

DataBinder.Eval always returns a string, so wrapping it in CStr() (as you've
done below) is unnecessary.

DataBinder.Eval takes 2 or 3 arguments. The first arg is the data object to
bind to. In the case of DataGrid, DataList and Repeater,
Container.DataItem is the single row. The second arg the *string* name of
the field from the data object you which to display. DataBinder.Eval uses
these two pieces of information to work out the rest of the expression
syntax.

An optional third argument is a .NET formatting string. DataBinder.Eval
will handle a single replacement for {0} only. So in your example
below:

<a href='<%# "default.aspx?CategoryId=" +
Cstr(Databinder.Eval(Container.DataItem, "ID"))%>'>
could be simplified to:
<a href='<%# Databinder.Eval(Container.DataItem,
"ID","default.aspx?CategoryId={0}" ) %>'>


Best of all, the Databinder.Eval syntax is the same for VB and C#.


hth,
Susan

------ End ------------

HTH, Daniel.

"Beginner" <bzh...@hotmail.com> wrote in message
news:#eoT9CWxBHA.2516@tkmsftngp04...

Beginner

unread,
Mar 7, 2002, 12:27:47 AM3/7/02
to
Thanks for everyone's help, I have better understanding now.

Actually I added quite some <%# functions %> to spice things up, hope they
wouldn't drag the server down. I used to RDS programming where most of the
process are on client side so you can do lots of fancy stuff without hitting
the server.


"Beginner" <bzh...@hotmail.com> wrote in message
news:#eoT9CWxBHA.2516@tkmsftngp04...

0 new messages