Now that we've discussed how to obtain data from a connection using a data reader object, we'll advance to the related subject of displaying the data on an ASP.NET page. We'll cover four data-aware controls that display multiple data values: list boxes, drop-down boxes, radio controls, and check boxes. After that, we'll examine the most commonly used options for the DataGrid, with the rest discussed in the next chapter, we'll cover controls that display single values, such as text boxes and labels. As you've seen in the examples we've looked at so far, ASP.NET offers an easy, three-step way to display data that comes from ADO.NET.
Rather like the situation we had with data reader objects, we've already used these steps for binding to a DataGrid object, but we're going to take the time now to look at the technique in more detail.
To begin, we ought to investigate something that may have been bugging you since we first came across it: what, precisely, is binding? Given that we can simply set the control's DataSource property, isn't the process of binding redundant? In fact, the two steps address two different issues, as the following example will demonstrate. Imagine that you have three text box controls that show three fields of the current record. The data source defines which field to display in each text box, while the binding means that the text box should display the value of the current record, and that the value should change when the current record changes. By having those three text boxes bound to the same record navigation system, when the user clicks to the next record, all three of the bound text boxes will show data from the next record.
Keep in mind two basic points about the syntax for all of the data binding controls we'll discuss in this chapter. First, these controls are ASP.NET only - you must use the <asp:> syntax. Second, they must run at the server, so they need the attribute runat="server" in the tag. ASP.NET provides about a dozen data-aware web server controls. These form two groups: those that display a single data value (such as a text box), and those that display multiple values (such as a list box or a data grid). The techniques for specifying the data to be displayed vary between the two types; we'll cover multiple-value controls in this chapter (because they're well suited for use with data readers), and we'll discuss the single-value controls in the next chapter (in conjunction with datasets). The most commonly used multiple-value controls are:
And when adding any of these controls to your page, you need to follow the following four steps:
In the sections that follow, we'll practice this pattern in examples that illustrate each of these control types. |