6

Which method is better (performance-wise) if I have DataBoundControl such as GridView, Repeater, and/or DataList and I use the following method to display data:

Eval("ColumnName")

or handling the ItemDataBound or RowDataBound event like:

void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{
    // my code to display data here
}

I prefer the second one for code readability reason, but for performance reason, are they the same (or are they even the same thing)?

Arief
  • 6,055
  • 7
  • 37
  • 41

3 Answers3

5

I also prefer the second version. It's easier for debugging and separation of html & code in my opinion.

According to this older doc, Improving .NET Application Performance and Scalability, it's more efficient (mentioned on page 297).

dev_feed
  • 689
  • 2
  • 7
  • 25
Lareau
  • 1,982
  • 1
  • 26
  • 47
  • 1
    +1: ease of debugging seals the deal for me. I never enjoy having to refactor or debug someone else's code where everything was bound in markup. Well, okay, I do get *some* pleasure out of the refactoring effort since I'm ripping all that crud out :) – Ahmad Mageed Nov 04 '10 at 14:03
  • 1
    Another advantage is the ability to manipulate the data before you put it on the page. It can be done in the .eval way but it can get really messy. – Lareau Nov 04 '10 at 23:59
2

Eval might be faster(depends on the situatiuon, because it is also latebound and uses reflection), but via DataBound-Event it is more readable and more future-proof.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Currently only what google gives me: http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet/browse_frm/thread/a6dca153057f2aa0?hl=en&lr&ie=UTF-8&rnum=7&prev=/groups?hl%3Den%26lr%3D%26ie%3DUTF-8%26q%3D%2522real%2Bworld%2Basp.net%2522%26btnG%3DSearch%26meta%3Dgroup%253Dmicrosoft.public.dotnet.framework.aspnet.*&pli=1 and http://msdn.microsoft.com/en-us/library/ms178366.aspx – Tim Schmelter Nov 04 '10 at 14:46
0

I think each are useful in situations. The eval way is a quick and easy way to do something simple if not much logic is needed. But, I've seen it abused by unexperienced programmers.

I recently took over development for a project where they were using a repeater. They were doing something like the following:

    <asp:Repeater>
      <ItemTemplate>
        Field Visible = '<%# (method(dataItem.a)) %>
        Field
        Field Visible = '<%# method(Eval(dataItem.a),Eval(dataItem.b)) %>'
        Field
        Field Visible = '<%# (method(dataItem.a)) %>'
        Field Visible = '<%# (method(dataItem.b)) %>'
      </ItemTemplate>
    </asp:Repeater>

This was just painful to look at. It looked like the developers just kept on adding more fields and calling the same method to check if the field should show or not. Something like this could easily be handled in the ItemDataBound and is way easier to upkeep. And you should always program with the expectation that your code will need to be modified/added on to later.

A Kimmel
  • 176
  • 1
  • 6