Monday, October 4, 2010

Which DataBound Control to use?

ASP.NET offers a rich set of data-bound controls. This variety of controls lets you choose the control that best suits your needs. At the same time, it might be confusing which control fits the job. Deciding which control to use depends on the functionalities the developer is going to offer the user. For example, if the user is supposed to have editing ability, certainly you cannot use the Repeater control.

Here is the list of the data-bound controls in ASP.NET:
  • GridView
  • DataList
  • DetailsView
  • FormView
  • ListView
  • Repeater
GridView is one of the commonly used data-bound controls developers mostly use. It provides a number of default functionalities that make common operations easy to implement with much less code than ever (if any!). You can edit a row or delete it, page through the records or sort rows based on a columns. GridView is the successor to DataGrid control. If you want to see a comparison between GridView and DataGrid controls, see http://msdn.microsoft.com/en-us/library/05yye6k9.aspx.

DataList is another data-bound control for displaying data as a table. It's a template-driven control and provides different templates to display data in normal, alternate, edit or select mode. You can also have templates for header and footer.
You can edit or delete rows in DataList, but the big disadvantage that makes me avoid using DataList is that it does not utilize the capabilities of data source controls for editing or deleting data. If you opt to use this control, you should consider that you have to write codes to handle data modification. I think generally the capabilities offered by the other data-bound controls saves you from using such a cumbersome control. I even wonder why they bothered to create it and add it to the list of data-bound controls!

DetailsView is a control usually used along with GridView control in master/detail scenarios. In this case, the selected row in the GridView control will be displayed in the DetailsView control.
DetailsView shows a single row at a time and provides paging, inserting new records, updating and deleting rows. It uses a table-based layout.

FormView is very similar to DetailsView, except that it's a template-driven control and doesn't provide a pre-defined layout. It contains several templates that allows developers to design the layout used for displaying the data.

ListView is a template-driven data-bound control. Like other template-driven controls, it uses several templates you can use to customize the layout of the data that is to be displayed. Unlike DataList control, it utilizes capabilities of the data source controls for common operations such as editing, inserting and deleting and also functionalities like sorting and paging.
Because it's the only control that implements IPageableItemContainer interface, it's the only one that can use DataPager control.

Repeater is used to create a read-only list of data rows. It's a template-driven control and allows you to customize the design of the displayed records by using templates. It has no default layout.

To wrap it up, whenever you reach the point where you have to choose the best control that suits your need best, you should consider what you exactly need the control to do, what kind of functionalities you want it to offer and whether you want to give it a customized look at design time or not.

No comments:

Post a Comment