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

Optimal Alternative To DataBinder.Eval

By , 9 Mar 2011
 
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)

About the Author

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralHi Richard Deeming. You are right, thanks for the valuable c...memberAlbinAbel15 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...memberRichard Deeming14 Mar '11 - 10:50 
In your DataReader example, you can use the same indexer syntax you used with the DataRowView example:
 
<%# ((DbDataRecord)Container.DataItem)["Name"] %>
<%# ((DbDataRecord)Container.DataItem)["Age"] %>

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 9 Mar 2011
Article Copyright 2011 by Albin Abel
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid