Binding ASP.NET Controls to Data
Data binding is crucial in Web applications, and ASP.NET's data-binding capability far surpasses ASP's.
October 24, 2001
ASP.NET’s data-binding capability far surpasses ASP’s
Most software applications, especially Web applications, report data. Web applications’ typical schema follows the 3-F pattern: fetch, format, and forward. You fetch data from a data source; format the data into a nice-looking page layout, paying attention to various heterogeneous factors such as user-friendliness, scalability, and code maintenance; and forward results to eagerly waiting client-side users. The process is similar on the receiving end, although the schema follows the 3-P pattern: pick up, process, and post. You pick up data from input forms on the client and send the information back to the Web server for further processing; process the collected data and prepare commands; and post data to a data store—normally a commercial database management system (DBMS).
In Microsoft .NET, Web pages consist mostly of server-side controls that make possible a modern, object-oriented programming style. But can you program the controls to make them sensitive to data sources? And, more important, can you do so automatically, with limited programming effort? Of course programmers would heartily welcome a programming-free method for associating rows of data with controls such as drop-down lists and grids. Data-bound controls are .NET’s solution to the growing demand for automatic binding between data sources and controls.
In general, data binding is the process that retrieves any data in any source and dynamically associates the data to a property on a control. This definition perfectly fits ASP.NET’s overall design and type of server-side components. ASP.NET’s implementation of data binding retrieves any data in any .NET-compliant data source. ASP.NET's data binding then lets you selectively manipulate and assign the data to properties on a control. Valid target controls are those specifically designed to support data binding.
ASP.NET provides two flavors of data binding: simple and complex. Simple data binding is a one-to-one (1:1) association between a control property and a data-source value (e.g., a certain field on a certain row). Complex data binding is a one-to-many (1:M—or many-to-many—M:N) association between one or more control properties and one or more data-source columns. In some cases, and with particular controls, the association is even tighter and the bound data source largely determines the control’s entire interface. This situation occurs, for instance, with the DataGrid control and a DataTable object.
In the past few years, many Active Server Pages (ASP) programmers developed ad hoc routines to generate boilerplate HTML code and populate drop-down lists or tables with information read from a database. In ASP.NET, this method is no longer necessary because the framework supplies plenty of components, including data-bindable drop-down, checkbox, and radio-button list controls.
You can use a suite of properties, including Text, DataSource, DataValueField, and DataTextField, to bind ASP.NET controls to a data source. Not all controls support all the properties. Which properties a data-bound control supports depends mostly on the control’s nature and structure. For example, text boxes support only the Text property. In contrast, a listbox control exposes the DataSource property to bind to an item list. The control also exposes the DataValueField and DataTextField properties to determine which data-source field to use to fill the control’s value and text attributes.
The DataSource property plays a key role because it lets you specify the data-source object the control binds to. Setting the DataSource property doesn’t immediately result in a caching operation. The control physically binds to the source data only when you call the method DataBind on the control or on the page that hosts the control. When the DataBind method runs, the control reads data from the associated data source. The control caches the data internally, in its Items collection property. Next, the control evaluates data-bound properties and redraws the UI to reflect changes.
A data source is represented by a .NET class but not necessarily by a repository class, whose contents come from a database table. In general, a .NET object that exposes the ICollection interface is a potential data-bindable source. The ICollection interface defines size, enumerators, and synchronization methods for all .NET collections. You can bind an ASP.NET control to a variety of data structures, including arrays, dictionaries, DataTable objects, and DataView objects. If you bind a list control to a DataSet object—namely, to a collection of container objects—then you also need to use the DataMember property to select a member. XML documents aren’t directly bindable. However, you can load an XML document into a DataSet object and bind the object. Alternatively, you can parse the XML document and bind the collection of nodes you obtain.
You use the DataSource property to bind list controls, such as DataGrid, Listbox, and DropDownList. For simpler controls, to bind properties to data you use a special expression that the DataBind method evaluates. A data-binding expression is text wrapped by <% … %> and prefixed by the number sign (#). You can include data-binding expressions as an attribute value in a server control’s opening tag or anywhere else on a page. You can’t programmatically assign a data-binding expression to any of the ASP.NET controls’ properties. The following code contains an example of a data-binding expression.
Within the delimiters, you can invoke user-defined functions, properties, and methods of other page components. The example shows a label control whose Text property is bound to the name of the currently selected element in a drop-down list control. The data-binding expression processor supports a minimal set of operators, including those to concatenate subexpressions. For more advanced processing, employ a user-defined method that's callable within the page.
Data binding is crucial to Web applications. Developers need to use information they obtain from databases to assign controls. The more easily and effectively developers can use the surrounding framework’s support tools to accomplish this task, the better. From this point of view, ASP.NET data binding is a quantum leap from ASP.
About the Author
You May Also Like