Implement Data-Bound Controls

There are a bunch of controls that can be bound to a data source in ASP.NET. In this section, we’ll cover the ones that sit in the Data tab in the toolbox, and are regarded as composite controls (e.g. not the DropDownList). I won’t explain every aspect and the usability of these controls, just the main differences between them. For a detailed tutorial of how to use them, visit the ASP.NET Data Access Tutorial site.

DataGrid: DataGrid is a good solution to show data in a tabular fashion, whit paging capabilities and a controllable layout with command fields, etc. However, if you are using ASP.NET 3.5 or later, you should consider using the GridView control, because it is regarded as the successor of the DataGrid.

GridView: same as the DataGrid control, it renders records from a data source, every record a row in a table. It supports massive layout personalization, like BoundField, CommandField, TemplateField, ButtonField, etc. It also supports selecting, editing and sorting data. It can be a good choice as a master table when implementing master-details scenarios. You can implement custom scenarios with a TemplateField. You can also assign templates to the GridView.

DataList: DataList is a template control, which shows the records of a data source as a list. You can predefine a template for how your data should be presented, like a FormView. The most important settings of a DataList (for the exam) are RepeatColumns, which lets you specify how many records are presented at the same time, RepeatDirection, to set how to display them, and RepeatLayout, to render the content as an HTML table, or not. Also supports selection and edit, but not in the standard manner, you must use commands to create the same effect.

Repeater: same as the DataList, but cannot set repeat direction, number of rows, etc. You can specify templates for this one, too, but it isn’t able to handle selection and editing the data. A common usage of the repeater tool is:

<asp:Repeater runat=”server”>











Notice that the BulletedList also provides a similar functionality.

DetailsView: the DetailsView control renders one record from a data source at a time, in a table form. It renders the entry in such a manner that each row represents a column in that entry. It supports paging, inserting, deleting and updating the current item. It’s very popular in master-detail scenarios, with a GridView.

FormView: the FormView is a template control, capable of rendering one entry at a time, with paging, editing, inserting and deleting. You can build up the rendering layout entirely from scratch, so it’s a good choice when you want complete control over the layout of the data presented.

ListView: displays the rows of the data source with predefined templates. Also supports edit, insert, delete, sort and paging data. A considerable feature enabled by ListView is the grouping of data.

DataPager: the DataPager allows you to add paging functionality in controls that implements the IPagableItemContainer interface, like the above mentioned ListView. To use it with this type of control, just add it to the controls layout (for example, in a template), or set the DataPager’s PageControlID to the target. The DataPager control has fields to defined templates for it, such as NextPreviousPagerField, NumericPagerField, or TemplatePagerField.

We have two data-bound controls left, which are slightly different from those mentioned above. These controls are inheriting from the HierarchicalDataBoundControl base class, and they are the Menu, and the TreeView controls. Usually you can use them to implement site navigation.

TreeView: displays hierarchical data, such as an XML document, in tree, nodes and leaves. You can use it to site navigation in conjuction with the SiteMapDataSource control, nodes set to display as hyperlinks. If you need additional information about the TreeView, visit this MSDN site.

Menu: menus can be used as data-bound controls too. The most popular scenario is to bind them to a SiteMapDataSource control. When you need some customization (such as when you want to show different menus for different roles), you can bind it to an XML file, using the DataBindings tag. Menus can (and should) be highly customized, but the names of these are so many, and speak for themselves that I wouldn’t like to explain them here. For a greater understanding of Menu controls, visit the MSDN.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s