ASP.NET Data Bound Controls
In this tutorial you will learn about Data Bound Controls – The Hierarchy of Data Bound Controls, Simple Data Bound Controls, Composite DataBound Controls and Hierarchical Data Bound Controls.
The Hierarchy of Data Bound Controls
Data Bound controls are controls that are bound to data sources. Traditionally the DataGrid is the principal data bound control in ASP.NET 1.x. Though DataGrid is still supported, ASP.NET 2.0 introduces three new controls—GridView, FormView and DetailsView.
Unlike in ASP.NET 1.x, all controls descend from the BaseDataBoundControl class. It has two basic child classes DataBoundControl and HierarchicalDataBoundControl. While TreeView and Menu are examples of the latter, AdRotator, ListControls such as BulletedList, CheckboxList, DropDownList, ListBox and RadioButtonList, and CompositeDataBoundControls such as DetailsView, FormsView and GridView are examples of the former.
All Data bound controls can be classified into Simple, composite and hierarchical controls. The DataBoundControl Base class defines the common characteristics of non-hierarchical controls which share the same base class. It is inherited from the WebControl and has all the visual and style properties of the base class. Additionally it has infrastructural properties such as Context, Page and AccessKey.
DataMember property of the control selects the list of data that the control has to bind to when the DataSource contains more than one list. DataSource indicates the source of the data it has to bind to. It is imperative that the DataSource must be an object (unlike ASP.NET 1.x) that implements the IEnumerable or the IListSource interface. DataSourceId is the ID of the data source object used to retrieve data.
The DataBoundControl class has only one method—the DataBind method. This method is called when data has to be pumped out of the data source. The internal implementation of this method has been tweaked and modified to enhance performance. It takes into account the implementation of the IEnumerable based data sources but is more sophisticated in execution as it implements the new interfaces and new data source controls also.
Simple Data Bound Controls
List based user interface controls have been classified as Simple Data bound controls. ASP.NET 2.0 has two simple data bound controls—the AdRotator and the list control.
The AdRotator control retrieves information from a XML file which contains the path to the distinctive image, the URL to go to when the control is clicked and the frequency of the Ad. Arbitrary data sources are supported by the control in ASP.NET 2.0 and it is fully bound to the data source. This control has additional capability of creating popup and pop under ads apart from standard banners. It supports counters for tracking purposes and updates the ad when the counter is clicked. Each ad can be associated with a separate counter.
The BulletedList control is a new addition to list controls in ASP.NET. It can be used to create a list of formatted list items. Individual items can be specified by defining a ListItem for each object. The bulleted lists are filled in from the data source and receives the DataTable returned by a query. The DataTextField property of the control selects the column to show and the DisplayMode sets the display mode. The Hyperlink mode links the page directly to an external URL and click is handled by the browser. The LinkButton mode click is handled by the ASP.NET runtime and fires a server side event. The OnClick handler will have to be defined in this instance. The Index property of the BulletedListEventArgs class contains the base index of the clicked item. The BulletStyle property controls the customization of the bullet styles.
There is no change in the other list controls in ASP.NET 2.0.
Composite DataBound Controls
Two new base classes have been added to enhance the Composite Data bound controls. The new CompositeControl and CompositeDataBoundControl are separate classes with a similar blueprint. The CompositeControl class addresses the UI-based needs and CompositeDataBoundControl defines the common foundation for all composite data bound controls.
The CompositeDataBoundControl is an abstract class that declares and implements the Controls property. In ASP.NET 1.x the Controls property stores the references to child controls. The CompositeDataBoundControl additionally exposes the CreateChildControls property. It takes a Boolean argument and behaves in accordance with the argument taken to ignore or initialize the view state. In ASP.NET 2.0 the developer has to only call the PerformDataBinding method and all boilerplate tasks are performed and the implementation of the CreateChildControls is called to implement the building of the control tree. We shall see examples of the GridView, the DetailsView and the FormView controls a little later in this tutorial.
Hierarchical Data Bound Controls
The HierarchicalDataBoundControl class is the base class for hierarchical data bound controls such as TreeView and Menu. It is an abstract class but does not have any predefined services. It behaves like a logical container for controls that consume the hierarchical data.
The TreeView control displays a hierarchy of nodes. Each node may contain child nodes. Parent nodes can be expanded to display child nodes or collapsed. Checkboxes can be displayed next to nodes or images from an imagelist control can be displayed. Nodes can be programmatically selected or cleared.
The key properties of the TreeView control are Nodes and SelectedNodes. The Nodes property defines the top level nodes in the TreeView. The SelectedNode property sets the currently selected node.
The TreeView control binds to any data source object that implements the IHierarchicalDataSource interface. It also exposes a DataSource property of the type object which can be assigned to an XmlDataDocument or to plain XML. By default the nodes are bound to its own nodes to reflect the name of the node rather than the attribute or the inner text. The node to node association can be controlled by binding parameters. The nodes can be bound to a data source field by specifying tree node bindings. The TreeNodeBinding object defines the relationship between each data item and the node it is binding.
The Menu control is an end to end site navigation tool. It can be bound to any data source and also supports explicit list of items for simple cases. The menu items are stored in a collection and the MenuItems collection property returns all the child items of a given menu. A few static and dynamic methods are supported by this class such as selected item, submenus and mouse over items. Dynamic menus are implemented using the Dynamic HTML object model.
In this section of the tutorial we have examined in some detail the various kinds of Data Bound controls and listed out the general features of each of these controls. In the following sections we shall experiment with the implementation of these controls on the web page of a Web application.