Introduction to Data Binding in ASP.NET


Data binding refers to the act of "binding" user interface elements to underlying data through some means. This chapter does not focus much on how previous versions of ASP.NET accomplished data binding except to briefly compare and contrast. Although the new ASP.NET controls can be bound the "old way" as well as the new ASP 2005 way, you will see a huge improvement in productivity by writing your code to take advantage of some of the new features. This section introduces you to the data source pattern that exists in both ASP 2005 and Windows Forms and how to create data sources. Finally, this section will finish up with an overview of the wide variety of controls available to you for using as-is or for extending through inheritance.

The Data Source Model

Data sources are just that: sources of data. The new data source model is designed in such a way that you can create a data source for any type of underlying data, and the UI control can then bind to the data source and not directly to the data. This allows the UI control to focus simply on rendering data and not on the underlying format of the data. For example, using various data sources you can now bind a single control to an XML document, to a DataSet, to a strongly typed collection of objects, to the results of a stored procedure execution in SQL, or to the results of an OLEDB query, and much more.

The steps required to connect your controls to a data source are extremely simple. First, you create your data source (discussed shortly), and then you drag a bindable control onto the form, select the data source from the drop-down box, and you're well on your way to creating a rich, interactive, and data-driven web page.

Creating Data Sources

Creating a data source is a simple task. After you've created an empty website through Visual Studio 2005, create a new Web Form. To create a data source, you drag one of the five built-in data source components onto your form. The following is a brief description of the data source components that come with ASP.NET:

  • SqlDataSource A data source used to access data contained in a SQL Server 2000 (or higher, including SQL 2005) database.

  • AccessDataSource A data source used to connect to data contained in a Microsoft Access (.mdb) file.

  • ObjectDataSource An extremely powerful data source component that allows you to connect a UI control to an arbitrary object.

  • XmlDataSource A data source used to connect to data contained within an XML document.

  • SiteMapDataSource A data source designed to connect to the data representing a site map.

After you drag the data source onto the form, the Data Source Configuration Wizard dialog will appear. This wizard will walk you through the process of configuring all the aspects of your data source. This includes specifying the query that obtains the data as well as additional queries (or stored procedures, if applicable) used for Create, Update, and Delete operations. With those operations defined, you can then use the stock ASP.NET controls to create, retrieve, update, and delete data from your data source whether the data is in an XML file, a SQL database, an Access database, or an instance of an object within your application.

To see this in action, open the Toolbox panel in Visual Studio and drag an AccessDataSource component onto your form. This should automatically bring up the smart tag menu. From here, click the Configure Data Source option. This will bring up a screen asking you for the filename of the Access database to which the data source will attach. Browse to the location of your Northwind.mdb file (this file is included with Visual Studio 2005 in one of the sample directories) and click Next, bringing up the screen shown in Figure 27.1.

Figure 27.1. SELECT statement configuration for an Access data source.


Select each column specifically (instead of clicking the asterisk) and then click Next to move to the next step in the wizard. The next step allows you to click Test Query to see a preview of the data that will be appearing in the control. After you've tested the query, click Finish and the Data Source Configuration Wizard will finish.

Drag a GridView control onto the form, autoformat it however you like, and set the data source to the Access data source you just created (named AccessDataSource1 by default). When you run this page in debug, you will see output that looks like Figure 27.2.

Figure 27.2. A GridView connected to an Access data source.


This is great, but it only allows you to view the data. The GridView also allows you to update and delete existing rows. The great thing about data sources is that the Create, Retrieve, Update, and Delete commands are all stored in the data source, and the control need not have any knowledge of those commands. To configure your data source for insert, update, and delete operations, go back to the Configure Data Source Wizard and click "Next" to get to the screen where you choose the columns for your SELECT statement. Click the Advanced button and you will see the dialog shown in Figure 27.3, which asks if you want to autogenerate UPDATE, DELETE, and INSERT statements.

Figure 27.3. Dialog prompting to autogenerate UPDATE, DELETE, and INSERT statements.


Before you think this is actually going to work, go into the properties for the data source and take a look at the InsertQuery, UpdateQuery, and DeleteQuery properties. There is an extremely useful miniature query builder that will pop up when you open these properties. More often than not, autogenerated queries are either not efficient enough or simply don't work at all. However, they are a good starting point for you to go in and optimize them to work properly. After a small tweak to make it work properly, the screenshot shown in Figure 27.4 shows a functioning update query.

Figure 27.4. A functioning update query for an Access Data source.


The only thing that a GridView can't do is utilize the InsertQuery property of the data source. This section won't get into details on how to do that because the section "Using Data-Bound Controls" later in this chapter covers utilizing the various controls that can be data-bound.

Data-Bound Control Hierarchy

Almost every single control that comes with ASP.NET 2.0 can be involved in the data-binding process in some way. However, there is a smaller list of controls that are designed specifically for working in a data-bound fashion, whether it is to display and edit the details of a single row of data or to display and edit multiple rows in a sorted, paged, or even filtered way. This list is shown in Figure 27.5.

Figure 27.5. Hierarchy of controls that support data binding.


The following is a brief overview of the controls shown in the hierarchy in Figure 27.5. The next section of this chapter will show you how to use each of these controls.

  • AdRotator A standard control for rotating advertisement information from a data-bound source

  • ListBox A standard list box control that displays multiple elements at a time, and can allow single or multiple selection of items

  • CheckBoxList A list of checkboxes that can be rendered in several modes from a data source

  • DropDownList A traditional "combo box"-style control whose elements and currently selected item can be pulled from a data source

  • RadioButtonList A list of radio buttons that can be rendered in several modes from a data source

  • DetailsView A composite control that can render a detail view showing the columns and values for a specific data row

  • FormView A composite control that renders an interactive form populated by a data source

  • GridView A grid-style view of data from a data source that is extremely powerful and can support in-line edit and deletion of data



Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net