C# – Should one bind data with Eval on aspx or override ItemDataBound in code-behind

asp.netc

For data bound controls (Repeater, ListView, GridView, etc.), what's the preferred way of binding data?

I've seen it where people use Eval() directly on the aspx/ascx inside the data bound control to pull the data field, but to me, it just seems so…inelegant. It seems particularly inelegant when the data needs to be manipulated so you wind up with shim methods like <%# FormatMyData(DataBinder.Eval(Container.DataItem, "DataField")) %> inside your control.

Personally, I prefer to put in Literal controls (or other appropriate controls) and attach to the OnItemDataBound event for the control and populate all the data to their appropriate fields in the code-behind.

Are there any advantages of doing one over the other? I prefer the latter, because to me it makes sense to compartmentalize the data binding logic and the presentation layer. But maybe that's just me.

Best Answer

I suggest you to go through this SO Question : Eval and ItemDataBound or RowDataBound event to display data, which one is better?

Base on the performance comparsion on the title : Real World ASP.NET Best Practices Using inline format expression mechanism (i.e. tag) is better than event hanlder mechanism (via ItemDataBound).

As i explored about this that the DataBinder.Eval syntax uses reflection and should be avoided if you can determine the object type at design time. Ref: Avoiding DataBinder.Eval in ASP.NET

Check this for Asp.net performance and scalibilty :

Related Topic