DataBinder.Eval vs explicit binding

Posts   
 
    
TlighT
User
Posts: 20
Joined: 19-Aug-2004
# Posted on: 19-Jan-2005 12:25:15   

As you probably know, DataBinder.Eval uses late-bound reflection which has a performance penalty over the explicit databinding syntax. Some articles claim DataBinder.Eval is 20% slower or more.

On the other hand, this code:

<%# DataBinder.Eval(Container.DataItem, "NewsItemContent[0].Title") %>

looks a lot nicer than this code:

<%# ((NewsItemContent)((NewsItem) Container.DataItem).NewsItemContent[0]).Title %>

An alternative is to have a function in the codebehind to do the casting:

protected NewsItemContent GetItemContent(object dataitem)
{
    return (NewsItemContent) ((NewsItem) dataitem).NewsItemContent[0];
}

and then:

<%# GetItemContent(Container.DataItem).Title %>

I think perhaps the last option is probably the best one, since you have both early binding and reasonably looking code in the aspx page.

What do you use?

cmartinbot
User
Posts: 147
Joined: 08-Jan-2004
# Posted on: 19-Jan-2005 14:23:10   

I personally use OnItemDataBound in codebehind when available. You can find your controls by name and bind them using the current DataItem. Then I don't have to worry about the HTML guys screwing up my app. wink