Click here to Skip to main content
Click here to Skip to main content

Using Explicit Cast instead of Databinder.Eval to Increase Performance

, 27 Nov 2012
Rate this:
Please Sign up or sign in to vote.
Increase ASP.NET Application performance with explicit casting

Introduction

The DataBinder.Eval method is used to bind data to a control's template. DataBinder.Eval casts Container.DataItem to its specific type, like this:

<ItemTemplate>
<div>
<%# DataBinder.Eval(Container.DataItem,"UserName") %>
</div> 
</ItemTemplate>  

DataBinder.Eval uses .NET reflection to cast Container.DataItem to its specific type which results in a performance loss. So it is better to use explicit casting.

Option 1 - Cast the Container.DataItem as a DataRowView if the data source is a DataSet.

<ItemTemplate> 
<div><%# ((DataRowView)Container.DataItem)["UserName"] %>
</div>
</ItemTemplate>

Option 2 for DataReader

<ItemTemplate>
<div>
<%# ((DbDataRecord)Container.DataItem)["UserName"] %>
</div>
</ItemTemplate> 

Cast the Container.DataItem as a String if the data source is an Array or an ArrayList.

<ItemTemplate>
<div>
<%# ((String)Container.DataItem)["UserName"] %>
</div>
</ItemTemplate>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Fenil Desai
Software Developer (Senior)
India India
IT Analyst/ Senior Software Engineer with 4+ years of experience in MS.NET Technologies
Follow on   Twitter   Google+

Comments and Discussions

 
QuestionMetrics? PinmemberMarcus Kramer27-Nov-12 4:00 
AnswerRe: Metrics? PinmemberFenil Desai27-Nov-12 19:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 27 Nov 2012
Article Copyright 2012 by Fenil Desai
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid