What's the difference between Binding Data to the repeater in the mark-up or binding it programatically
What's the difference between binding for example a column called ("Name") in both cases ? and is there's any performance difference ?
1- Assigning the data in the mark-up
<asp:Label ID="Name_Lbl" runat="server" Text='<%# Eval("Name") %>' ></asp:Label>
2- defining a control object for every control inside the repeater ItemTemplate and find 开发者_如何学JAVAit and then assign the data in the column "Name" to it
e.Item.FindControl("Name_Lbl")
You will probably get the same results but performance may differ. Data-bind expressions (Eval
) uses reflection under the hood to bind the data while FindControl
will walk up the control tree to find the necessary control. Reflection does have some performance cost (depending upon how many properties/names you are looking up although once looked up a propery descriptor does get cached). On the other hand, overhead of control tree walk-up will depend upon how deep is the control tree.
IMO, data binding syntax is more elegant and I will prefer that - performance has to be seen in relative terms - how much extra I am spending as compared to total request time and so data binding cost is negligible compared to other activities such as actually fetching the data. Said that there are variations in data-binding that avoids reflection - see http://weblogs.asp.net/jgalloway/archive/2005/09/20/425687.aspx.
Many times, I use a code-behind property that provides strongly typed property for ease of use. For example, when repeater is bound with array/list/enumeration of entity class ProductDetails, in code-behind, I use method such as
protected ProductDetails GetProduct(RepeaterItem container)
{
return (ProductDetails)container.DataItem;
}
And in mark-up,
<asp:Label ID="Name_Lbl" runat="server" Text='<%# GetProduct(Container).Name %>' ></asp:Label>
Not to mention that you get intellisense working on GetProduct(Container)
as its stringly typed
No difference, .NET creates the same ControlTree on the back-end. This article explain whats going on under the hood of ASP.NET during compilation. It is an older article but relevant.
Compilation and Deployment in ASP.NET 2.0
精彩评论