Should one bind data with Eval on aspx or override ItemDataBound in code-behind?
Posted
by
George Chang
on Programmers
See other posts from Programmers
or by George Chang
Published on 2012-01-25T04:49:13Z
Indexed on
2012/11/17
11:25 UTC
Read the original article
Hit count: 365
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.
© Programmers or respective owner