Click here to Skip to main content
13,090,250 members (81,056 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 7 Apr 2010

Databinder.Eval using Lamda Expressions

, 7 Apr 2010
Rate this:
Please Sign up or sign in to vote.
Using lambda expression to help with compile time checking of Eval statements


We all have done it, and we all know how it can be frustrating when dealing with magic strings. While not terribly hard to fix or work around, the Databinder.Eval("") method needs to use strings in order to work. However, using some extension methods (or a helper class) can remove the dependency on these magic strings and allow your code to use lambda expressions instead.

Using the Code

public static class ExtensionMethods
    public static string Evaluate<T>(this ListViewItem item, 
	Func<T, object> expression)
        var result = expression((T)item.DataItem);
        if (result != null)
            return result.ToString();
        return null;

    public static TOutput Evaluate<T, TOutput>
	(this ListViewItem item, Func<T, TOutput> expression)
        return (TOutput)expression((T)item.DataItem);

So, with these, instead of using code that looks like this:

<asp:GridView ID="gvName" runat="server" AutoGenerateColumns="False"
         <asp:TemplateField HeaderText="County Name">
                 <%# Eval("County.Name") %>

You can use the slightly more verbose, but more stable lambda version:

 <asp:GridView ID="gvName" runat="server" AutoGenerateColumns="False" 

     <asp:TemplateField HeaderText="County Name">               
         <%# Container.Evaluate<AddressEntity>(c=>c.County.Name) %> 

Points of Interest

Just remember to include the namespace of wherever you placed your code either at the top of the page or in the web.config.

The second version of the Evaluate method allows you to return the data typed as it actually is instead of returning it as the string. This is useful if you need to call page methods and don't want to cast it once to a string with Eval(), then recast it back.

<%# DoSomething(Container.Evaluate<AddressEntity, int?>(c=>c.County.CountyID)) %>   


  • 04/07/2010 - Original draft


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


About the Author

Stephen Inglish
Software Developer (Senior) Harland Financial Solutions
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralUsing default behaviour Pin
Partenon10-May-10 22:53
memberPartenon10-May-10 22:53 
GeneralRe: Using default behaviour Pin
Stephen Inglish7-Jun-10 10:51
memberStephen Inglish7-Jun-10 10:51 
GeneralTwo-way binding Pin
IlyaD_Russia16-Apr-10 3:21
memberIlyaD_Russia16-Apr-10 3:21 
GeneralGreat! Very useful Pin
Lion_cl12-Apr-10 19:23
memberLion_cl12-Apr-10 19:23 

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
Web03 | 2.8.170813.1 | Last Updated 7 Apr 2010
Article Copyright 2010 by Stephen Inglish
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid