Exforsys

H I D E

Home arrow Technical Training arrow ASP.NET 2.0

ASP.NET Data Bound Controls

Author : Exforsys Inc.     Published on: 6th Sep 2005    |   Last Updated on: 18th Mar 2011

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.

Ads

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.

Read Next: ASP.NET GridView Control


 
This tutorial is part of a ASP.NET 2.0 tutorial series. Read it from the beginning and learn yourself.

ASP.NET 2.0

  1. Getting started with ASP.NET 2.0
  2. .NET Framework Fundamentals
  3. Microsoft.NET Framework Tools
  4. Application Development in .NET
  5. What's New in the .NET Framework 2.0 ?
  6. Introduction to Visual Studio.NET
  7. Installing Visual Studio.NET 2005
  8. Working with Visual Studio.NET Web Applications
  9. Whats New in ASP.NET 2.0
  10. Creating an ASP.NET Application
  11. ASP.NET Code Directory
  12. ASP.NET Page Object Model
  13. ASP.NET Server Controls
  14. ASP.NET Working With Master Pages
  15. ASP.NET Creating Content for Master Page
  16. ASP.NET Referencing Master Page Members
  17. ASP.NET Changing Master Pages Dynamically
  18. ASP.NET Creating Nested Master Pages
  19. ASP.NET Working with Web Parts
  20. ASP.NET Using Web Parts and Controls in Web Pages
  21. ASP.NET Web Pages and Layout
  22. ASP.NET - Adding Web Parts at Run Time
  23. ASP.NET Personalization: User Profiles and Themes
  24. ASP.NET Data Access features
  25. ASP.NET State Management
  26. ASP.NET Customizing the Session State Mechanism
  27. ASP.NET State Management And Caching
  28. ASP.NET Security
  29. Forms Authentication in ASP.NET
  30. ASP.NET Managing Membership and Roles
  31. ASP.NET Configuring Page-Level Caching
  32. ASP.NET Setting Application-Level Caching
  33. ASP.NET Data Source Object Model
  34. ASP.NET SqlDataSource Control
  35. ASP.NET Data Bound Controls
  36. ASP.NET GridView Control
  37. ASP.NET GridView Filtering
  38. ASP.NET Adding Sorting and Paging in GridView
  39. ASP.NET DataBound Controls - Details View
  40. ASP.NET Using a Grid to Display Detail Information
  41. ASP.NET Displaying Master-Detail Data on the Same Page
  42. Displaying Master-Detail Data on Separate Pages in ASP.NET
  43. ASP.NET Creating Web Wizards
  44. ASP.NET : Dynamic Image control
  45. ASP.NET Advanced Site Functionality
 

Comments