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

Tagged as

Optimal Alternative To DataBinder.Eval

, 9 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
It is not a trick, but a tip/ call for consider to use alternative to Databinder.Eval method with larger data sets
It is common practice among programmers to use DataBinder.Eval method in data binding controls. This should be acceptable for a small amount of data. But if the data become large, it is not an optimal method as it has the overhead of using reflection.
 
According to Microsoft:
"Because this method performs late-bound evaluation, using reflection at run time, it can cause performance to be noticeably slow."
 
Example:
 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataKeyNames="PersonId" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <%# DataBinder.Eval(Container.DataItem,"Name") %>
            </ItemTemplate>
        </asp:TemplateField>
       <asp:TemplateField>
            <ItemTemplate>
                <%# DataBinder.Eval(Container.DataItem,"Age") %>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
Optimal alternative:
 
<asp:GridView ID="GridView2" 
runat="server" AutoGenerateColumns="False"
    DataKeyNames="PersonId" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <%# ((DataRowView)Container.DataItem)["Name"]%>
            </ItemTemplate>
        </asp:TemplateField>
       <asp:TemplateField>
            <ItemTemplate>
                <%# ((DataRowView)Container.DataItem)["Age"]%>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
If Data Reader is used to bind the control, then the optimal alternative is:
 
<asp:GridView ID="GridView3" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <%# ((DbDataRecord)Container.DataItem).GetString(0)%>
            </ItemTemplate>
        </asp:TemplateField>
       <asp:TemplateField>
            <ItemTemplate>
                <%# ((DbDataRecord)Container.DataItem).GetInt32(1)%>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
For using these castings, you need to import a few namespaces to the page such as:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Common" %>

License

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

Share

About the Author

Albin Abel
Software Developer
India India
I am developer in .Net and GIS. albin_gis@yahoo.com

Comments and Discussions

 
QuestionHi Albin, can you please share if data source is object from entity model how can we use these alternative methods?? Pinmembernikhil_chavan28-May-13 4:05 
GeneralHi Richard Deeming. You are right, thanks for the valuable c... PinmemberAlbinAbel15-Mar-11 2:41 
GeneralIn your DataReader example, you can use the same indexer syn... PinmemberRichard Deeming14-Mar-11 11:50 

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 | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 9 Mar 2011
Article Copyright 2011 by Albin Abel
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid