Container.DataItem

R

rn5a

What's the difference between

<%# DataBinder.Eval(Container.DataItem,"LastName") %>

&

<%# Container.DataItem("LastName") %>

Thanks
 
A

Alexey Smirnov

What's the difference between

<%# DataBinder.Eval(Container.DataItem,"LastName") %>

&

<%# Container.DataItem("LastName") %>

Thanks

DataBinder.Eval is a helper function to evaluate data, and it use
reflection (late binding) to find right property in your item. You can
display data without using DataBinder.Eval, but you need to cast
Container.DataItem to the right type.

Note: Because DataBinder.Eval performs late-bound evaluation, using
reflection at run time, it can cause performance to noticeably slow
compared to explicit casting.
 
E

Eliyahu Goldin

I used to advocate explicit casting in this newsgroup a while ago. There was
a strong opposition to it from quite experienced developers claiming that
the performance gain is not that significant comparing with the advantages
of using Eval. The major one is that with Eval you can change your
datasource without touching your databinding expressions. I can hear this
argument very well.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
R

rn5a

DataBinder.Eval is a helper function to evaluate data, and it use
reflection (late binding) to find right property in your item. You can
display data without using DataBinder.Eval, but you need to cast
Container.DataItem to the right type.

Note: Because DataBinder.Eval performs late-bound evaluation, using
reflection at run time, it can cause performance to noticeably slow
compared to explicit casting.

Alexey, could you please show some examples of what you have
explained? Sorry I couldn't exactly follow the difference.

Thanks to both of you....
 
A

Alexey Smirnov

Alexey, could you please show some examples of what you have
explained? Sorry I couldn't exactly follow the difference.

Thanks to both of you....- Hide quoted text -

- Show quoted text -

Here's a good article about that difference
http://odetocode.com/Articles/278.aspx

DataItem returns a reference to an object, and to return a value in
the proper type you need to cast it (especially for C#). Maybe it
makes no sense when we are talking just about simple strings but it
can be useful when you have some casting, e.g. show an integer as a
currency, etc.

DataBinder.Eval allows to avoid using casts because it finds
dynamically a property and its type by the name at runtime.
 
T

Teemu Keiski

I agree. I've heard that same argument (about performance) from Nikhil
Kothari himself.

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net


Eliyahu Goldin said:
I used to advocate explicit casting in this newsgroup a while ago. There
was a strong opposition to it from quite experienced developers claiming
that the performance gain is not that significant comparing with the
advantages of using Eval. The major one is that with Eval you can change
your datasource without touching your databinding expressions. I can hear
this argument very well.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Alexey Smirnov said:
DataBinder.Eval is a helper function to evaluate data, and it use
reflection (late binding) to find right property in your item. You can
display data without using DataBinder.Eval, but you need to cast
Container.DataItem to the right type.

Note: Because DataBinder.Eval performs late-bound evaluation, using
reflection at run time, it can cause performance to noticeably slow
compared to explicit casting.
 
A

Alexey Smirnov

I agree. I've heard that same argument (about performance) from Nikhil
Kothari himself.

--
Teemu Keiski
AspInsider, ASP.NET MVPhttp://blogs.aspadvice.com/jotekehttp://teemukeiski.net

message

I used to advocate explicit casting in this newsgroup a while ago. There
was a strong opposition to it from quite experienced developers claiming
that the performance gain is not that significant comparing with the
advantages of using Eval. The major one is that with Eval you can change
your datasource without touching your databinding expressions. I can hear
this argument very well.
--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net

- Show quoted text -

My quote regarding performance was from here
http://msdn2.microsoft.com/en-us/library/4hx47hfe.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top