Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


October 25, 2001

Binding ASP.NET Controls to Data


RSS
Subscribe to Windows IT Pro | See More Active Server Pages (ASP) Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

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.

<asp:label runat="server" Text='<%# dropdown.SelectedItem.Text %>' />

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.

End of Article



Reader Comments
Could use more examples

Anonymous User December 10, 2004


Concur

Anonymous User December 10, 2004 (Article Rating: )


bvcbvc

Anonymous User March 14, 2005 (Article Rating: )


gffffff

Anonymous User March 14, 2005 (Article Rating: )


gfgfghg

Anonymous User March 14, 2005 (Article Rating: )


tyrty

Anonymous User March 14, 2005 (Article Rating: )


learn dreamweaver at
dmxtutorials.com

Anonymous User July 15, 2005 (Article Rating: )


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
CES 2009: Ballmer Announces Windows 7, Windows Live, Live Search Milestones

During his first-ever Consumer Electronics Show (CES) 2009 keynote address last night in Las Vegas, Microsoft CEO Steve Ballmer announced the pending public availability of a feature-complete Windows 7, the final version of Windows Live Essentials, and ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

Where is Microsoft NetMeeting in Windows XP?

...


SQL Server and Database Whitepapers StoreVault SnapManagers for Microsoft Exchange and SQL Server

Related Events SQL Server, Data Warehouse & BI Solutions!

Virtual Desktop Infrastructure (VDI): Technologies, Differences, Successes

Check out our list of Free Email Newsletters!

SQL Server and Database eBooks Safeguarding Your Windows Servers

SQL Server Administration for Oracle DBAs

Taking Control: Monitoring the Windows Platform Proactively

Related SQL Server and Database Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2009 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing