Click here to Skip to main content
12,761,018 members (31,489 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


20 bookmarked
Posted 19 Jul 2013

The Evil of Eval() in ASP.NET

, 19 Jul 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
"Eval is evil". If you have any dignity at all, as a programmer, you are now compelled to banish it forever.

I’ve come across this one several times when working with ASP.NET. When you want to put something from .NET onto your ASP.NET page, it is common to do one of these:

<%# Eval(“FirstName”) %>
<%# Bind(“FirstName”) %> //Bind is just a wrapper around Eval()

As opposed to something like this:

<%-- In C# --%>
<%# ((DataRowView)Container.DataItem)["FirstName"] %>

<%-- In VB.NET --%>
<%# CType(Container.DataItem, System.Data.DataRowView)("FirstName") %>

The second one seems like more typing. Otherwise they seem pretty close. So, a lot of developers figure: go with fewer words. It’s the kiss principle (keep it simply simple). Guess what? This simple way is a performance killer.

The Eval() command uses reflection to evaluate this expression. In case you are not familiar with reflection, it is bad. It is a notorious performance hog and you should avoid it whenever possible. Microsoft even warns you in the MSDN article about Eval ( read the section under “Note”).

Think of it like this: Eval takes about 1/20th of a second per call, and the other way takes about 1/1000th of a second per call. On a grid with 5 columns and 8 rows, Eval would take 2 seconds and the other would take under 1 second. Nobody would notice the difference. However, change that grid to 10 columns and 50 rows. You now have a difference of 25 seconds. It is the kind of thing that might not matter very much, most of the time but when it does, it makes a big difference.

Once, I had a discussion with a seasoned developer about this topic and he asked “If Eval() is so bad, why would Microsoft leave it in there and refer to it in so many of their docs and online examples?” The reason is: these docs are for newbies and rookies. People who are just starting-out seem to prefer the simplest ways of doing things.

Do you think you should give a rookie the task of building a demanding web page that needs to scale really well or requires peak performance? Someday, yes, but at first, the rookie just wants to get his stuff to run. He needs to crawl before he walks/runs. Knowing how to tune your apps to perform-well is a craft and it comes after experience. Some consider it a bit of a right-of-passage. The day you turn from your Eval() ways, is the day you become a man in the .NET world. In contrast, if you are still using Eval, then it is time for you to move out of your mom’s basement and start using strong-types. You don’t need reflection to do your heavy-lifting any more. You know what Type that databinder is using. So get it together and type-cast it like a senior developer and stop wasting all of those CPU ticks.

If you would like to read more, let me recommend the following articles:

So always remember: "Eval is evil". If you have any dignity at all, as a programmer, you are now compelled to banish it forever. If you ever see it in any code, anywhere, light a torch or grab a pitchfork and run it out of town like the monster that it is.


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


About the Author

Tim Golisch
United States United States
I have been programming for 30 years(22 professionally). I mostly work with the Microsoft product stack (.NET, SQL Server, IIS, ASP.NET, Dynamics, TFS, etc) however, I have worked with a wide variety of others too (IBM DB2, Oracle, Java, PHP, ColdFusion, Perl, etc). I have taught classes at community colleges, and other speaking engagements. During the day, I am a consultant, programming and fixing programs.

You may also be interested in...

Comments and Discussions

QuestionSome newbie-ish issue Pin
Member 1168765013-May-15 4:59
memberMember 1168765013-May-15 4:59 
AnswerRe: Some newbie-ish issue Pin
Tim Golisch15-May-15 3:35
memberTim Golisch15-May-15 3:35 
GeneralMy vote of 4 Pin
R.Hari Prasath26-Jul-13 1:10
memberR.Hari Prasath26-Jul-13 1:10 
GeneralRe: My vote of 4 Pin
Tim Golisch26-Jul-13 4:10
professionalTim Golisch26-Jul-13 4:10 
QuestionCode review Pin
RonDsz23-Jul-13 6:21
memberRonDsz23-Jul-13 6:21 
QuestionName 'DataRowView' is not declared. Pin
rogerperkins23-Jul-13 5:49
memberrogerperkins23-Jul-13 5:49 
AnswerRe: Name 'DataRowView' is not declared. Pin
Tim Golisch24-Jul-13 2:13
professionalTim Golisch24-Jul-13 2:13 
AnswerRe: Name 'DataRowView' is not declared. Pin
Tim Golisch9-May-14 6:55
professionalTim Golisch9-May-14 6:55 
GeneralMy vote of 5 Pin
Anurag Gandhi22-Jul-13 23:49
memberAnurag Gandhi22-Jul-13 23:49 
QuestionType the result? Pin
timh9999922-Jul-13 12:01
membertimh9999922-Jul-13 12:01 
AnswerRe: Type the result? Pin
KP Lee23-Jul-13 19:01
memberKP Lee23-Jul-13 19:01 
GeneralRe: Type the result? Pin
timh9999924-Jul-13 2:46
membertimh9999924-Jul-13 2:46 
QuestionWhy Not disucss the Security? Pin
CdnSecurityEngineer22-Jul-13 6:33
memberCdnSecurityEngineer22-Jul-13 6:33 
GeneralMy vote of 5 Pin
hypermellow22-Jul-13 4:02
memberhypermellow22-Jul-13 4:02 
GeneralMy vote of 5 Pin
itaitai21-Jul-13 23:35
memberitaitai21-Jul-13 23:35 
GeneralMy vote of 5 Pin
MBSMBS21-Jul-13 7:19
memberMBSMBS21-Jul-13 7:19 
GeneralRe: watch the Wall Pin
Tim Golisch21-Jul-13 17:46
professionalTim Golisch21-Jul-13 17:46 
QuestionMy vote of 5 Pin
blachsmith19-Jul-13 15:49
memberblachsmith19-Jul-13 15:49 
GeneralMy vote of 5 Pin
drewman515019-Jul-13 11:57
professionaldrewman515019-Jul-13 11:57 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 19 Jul 2013
Article Copyright 2013 by Tim Golisch
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid