Click here to Skip to main content
15,885,365 members
Articles / Web Development / ASP.NET
Tip/Trick

Optimal Alternative To DataBinder.Eval

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
9 Mar 2011CPOL 29.1K   2   3
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:

XML
<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:

XML
<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:

XML
<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)


Written By
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?? Pin
nikhil_chavan28-May-13 3:05
nikhil_chavan28-May-13 3:05 
GeneralHi Richard Deeming. You are right, thanks for the valuable c... Pin
Albin Abel15-Mar-11 1:41
Albin Abel15-Mar-11 1:41 
Hi Richard Deeming. You are right, thanks for the valuable comment
GeneralIn your DataReader example, you can use the same indexer syn... Pin
Richard Deeming14-Mar-11 10:50
mveRichard Deeming14-Mar-11 10:50 

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.