Using Data-based Controls in ASP.NET

Chapter 4 - Data Readers, Command Objects, and Web Server Controls
byJohn Kauffman, Fabio Claudio Ferracchiatiet al.?
Wrox Press ?2002

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.

  • Add an HTML element for a data-aware web server control (such as a text box or a data grid) to the ASP.NET page

  • Assign an ADO.NET data reader object (or some other ADO.NET object that implements the System.Collections.IEnumerable interface) to the control's DataSource property

  • Execute the DataBind() method of the control

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.

If you're one of those people who spent three years on classic ASP, typing their fingernails down to the quick to display data, the ASP.NET web server controls are an answer to your prayers. In most cases, you don't have to perform any looping, and you never have to be involved in typing <TH>, <TR>, and <TD> elements.

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.

When we're using a DataGrid that shows all the records at once, as we've been doing so far, this aspect of binding is not obvious - that's why we've had to clarify it. Later, however, we'll discuss DataGrid paging, which displays only some of the records at a time. In that situation, the fruits of binding are more obvious; it ensures that all fields display values from the same subset of records.

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:

  • <asp:ListBox> and <asp:DropDownList>

  • <asp:RadioButtonList> and <asp:CheckBoxList>

  • <asp:DataGrid>

And when adding any of these controls to your page, you need to follow the following four steps:

  • Add the ASP.NET control to your page, and identify the methods that are to be run in response to user events (such as clicks on the control)

  • Create your connection and command objects

  • Set the DataSource property, and run Bind() for the data control

  • Write code into the handlers for the clickable events

In the sections that follow, we'll practice this pattern in examples that illustrate each of these control types.



Beginning ASP. NET 2.0 and Databases
Beginning ASP.NET 2.0 and Databases (Wrox Beginning Guides)
ISBN: 0471781347
EAN: 2147483647
Year: 2004
Pages: 263

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