What is most efficient? OnItemDataBound or using a function

A

Anders

Hi, I was wondering what is most efficient of the two.

Is it more efficient to add server controls within the Itemtemplate and use
OnItemDataBound to manipulate and databind the servercontrols.

Or is better to send the DataItems via. a method/function from within the
Itemtemplate (<%# call function %>)

Id love to hear your thoughts.
Anders
 
T

Teemu Keiski

Hi,

I don't think there is much difference with these approaches, at least not
significant. The performance hit with databinding comes when you use
DataBInder.Eval in databinding expressions because it would cause
latebinding to be used and if you pass DataItem to custom function or use
built-in ItemDataBound you are all the-time running strongly-typed code
which can be optimized by the runtime.

You can think that ItemDataBound is called in any case. If you add your
custom function, it would be one slight step more to run, but as I said, the
difference is 0 in practise. All code is compiled and if you use it as typed
as possible, e.g avoid late-binding there's not much to think with it.
 
G

Greg Burns

From "Real World ASP.NET Best Practices" (ch.5)

Basically there are three options avaialable:

1.inline format expressions (ex. using DataBinder.Eval)
2.event handlers (ex. using OnItemDataBound)
3.member methods (ex. your method/funciton call)

Their conclusions (whith performance tests to back it up): members methods
perform much better than event handlers and just a fast as inline format
expressions.

They go on to give an example that is quite interesting: (Rather than a
member method for each dataitem, they make one generic one for the entire
datarow)

In aspx page code like

<td><%#ShowData(Container.DataItem, "OrderID")%></td>
<td><%#ShowData(Container.DataItem, "CompanyName")%></td>
<td><%#ShowData(Container.DataItem, "OrderDate")%></td>

In codebehind:

Protected Function ShowData(data as object, ColumnName as string) as string

Dim inputdata as DataRowView = CType(data, DataRowView)

Select Case ColumnName
Case "OrderID"
return inputData("OrderID").Tostring

Case "CompanyName"
return inputData("Compyname").Tostring

Case "OrderDate"
return (format inputData("OrderDate") however you want here)

Case Else
return ""

End Select
End Function

HTH,
Greg
 
A

Anders

What I can conclude from both your replies is to avoid latebinding and use
methods or event handlers. Also still, making one generic method for the
entire datarow should perform better than strongly define each dataitem like
this: <%# ((DataRowView) Container.DataItem) ["OrderID"]%>

Greg does the book say anything about why event handlers are slower?
 
G

Greg Burns

I would think <%# ((DataRowView) Container.DataItem) ["OrderID"]%> would
preform the same as using the generic method for the entire datarow, since
they both avoid late binding.

The generic method I showed has some benefites in that you can refer to the
other columns from within the same function.

It doesn't really say why the event handler is faster (at least I am not
seeing it right now). But it does mention having to use FindControl in the
event handler method, which could be a bottleneck. (IMO)

Greg


Anders said:
What I can conclude from both your replies is to avoid latebinding and use
methods or event handlers. Also still, making one generic method for the
entire datarow should perform better than strongly define each dataitem
like
this: <%# ((DataRowView) Container.DataItem) ["OrderID"]%>

Greg does the book say anything about why event handlers are slower?


Greg Burns said:
From "Real World ASP.NET Best Practices" (ch.5)

Basically there are three options avaialable:

1.inline format expressions (ex. using DataBinder.Eval)
2.event handlers (ex. using OnItemDataBound)
3.member methods (ex. your method/funciton call)

Their conclusions (whith performance tests to back it up): members
methods
perform much better than event handlers and just a fast as inline format
expressions.

They go on to give an example that is quite interesting: (Rather than a
member method for each dataitem, they make one generic one for the entire
datarow)

In aspx page code like

<td><%#ShowData(Container.DataItem, "OrderID")%></td>
<td><%#ShowData(Container.DataItem, "CompanyName")%></td>
<td><%#ShowData(Container.DataItem, "OrderDate")%></td>

In codebehind:

Protected Function ShowData(data as object, ColumnName as string) as string

Dim inputdata as DataRowView = CType(data, DataRowView)

Select Case ColumnName
Case "OrderID"
return inputData("OrderID").Tostring

Case "CompanyName"
return inputData("Compyname").Tostring

Case "OrderDate"
return (format inputData("OrderDate") however you want here)

Case Else
return ""

End Select
End Function

HTH,
Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top