Datasheet

Now that you have had a look at the data source controls supplied with ASP.NET 2.0, this section will
examine the data-bound controls that you will normally use to display data that is contained in the data
source controls. These data-bound controls bind data automatically.
One of the neat things about data-bound controls is that the development environment automatically
guides developers through the process of binding a data control to a data source. Developers are
prompted to select the particular data source to use for selecting, inserting, updating, and deleting data.
The feature that walks the developers through this process is called Smart Tasks. This is explained in
detail in the “Visual Studio 2005 Improvements” section later in this chapter.
The DataGrid is one of the most popular data-bound controls in ASP.NET, but in some ways it is a vic-
tim of its own success: It is so rich in functionality that it leaves ASP.NET developers wanting even more.
The
DataGrid control does not change much in ASP.NET 2.0, but the new GridView control offers fea-
tures commonly requested in
DataGrid and adds a few surprises of its own. In addition, new controls
named
DetailsView and FormView simplify the building of master-detail views and web-editable user
interfaces. The new data-bound controls introduced in ASP .NET 2.0 are:
<asp:GridView>: This control is the successor to the DataGrid control that was part of
ASP.NET 1.x, and is used to display multiple records in a web page. However, the
GridView
also enables you to add, update, and delete a record in a database without writing a single line
of code. Similarly to the
DataGrid control, in a GridView control each column represents a
field, while each row represents a record. As you would expect, you can bind a
GridView con-
trol to a
SqlDataSource control, as well as to any data source control as long as that control
implements the
System.Collections.IEnumerable interface.
<asp:DetailsView>: Can be used in conjunction with the GridView control to display a spe-
cific record in the data source.
<asp:FormView>: Provides a user interface to display and modify the data stored in a
database. The
FormView control provides different templates, such as ItemTemplate and
EditItemTemplate, that you can use to view and modify the database records.
<asp:TreeView>: Provides a seamless way to consume information from hierarchical data
sources, such as an XML file, and then display that information. You can use the
TreeView con-
trol to display information from a wide variety of data sources, such as an XML file, a sitemap
file, a string, or a database.
<asp:Menu>: Like the TreeView control, the Menu control can be used to display hierarchical
data. You can use the
Menu control to display static data, sitemap data, and database data. The
main difference between the two controls is their appearance.
Listing 1-2 shows you an example of how to use the combination of
SqlDataSource and GridView con-
trols to retrieve and display data from the ProductCategory table in the AdventureWorks database with-
out even having a single line of code.
Listing 1-2: Using the SqlDataSource Control to Retrieve Categories Information
<%@ page language=”C#” %>
<html>
<head id=”Head1” runat=”server”>
<title>Data Binding using SqlDataSource control</title>
</head>
(continued)
9
Chapter 1: Introduction to ASP.NET 2.0
05_041796 ch01.qxp 12/29/06 9:09 PM Page 9