Click here to Skip to main content
12,298,406 members (50,912 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

17.9K views
2 bookmarked
Posted

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

You may also be interested in...

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
membernikhil_chavan28-May-13 3:05 
GeneralHi Richard Deeming. You are right, thanks for the valuable c... Pin
AlbinAbel15-Mar-11 1:41
memberAlbinAbel15-Mar-11 1:41 
GeneralIn your DataReader example, you can use the same indexer syn... Pin
Richard Deeming14-Mar-11 10:50
memberRichard 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.

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