Are Grids All You Got?
Not by a long shot. Since the release of ASP.NET 1.0, third-parties have been providing developers with all kinds of great Web Controls to make our lives easier. Let's take a brief moment to bow our heads and thank them for all the code they've saved us.... OK, back to work. In Visual Studio 2005, Microsoft has increased the number of Web Controls that come with Visual Studio to include some improved replacements of old ones as well as some very interesting new ones.
The GridView Control
What better way to start a section titled, "Are Grids All You Got?" than to talk about what? Another grid of course! The GridView in ASP.NET 2.0 is a replacement for the DataGrid (though the DataGrid is still supported) and adds some functionality built into it that in the past, needed code from the developer. As useful as the DataGrid can be, adding paging and sorting functionality only changes some visual aspects of the grid and raises some events. The WebForm developer has to provide the code for changing the sort source or the page index, then rebinding the data. The new GridView performs all this for you with the simple setting of some properties. Think about what I talked about earlier and you'll realize that the magic behind this is that the code you once had to write at the page level has now been written by Microsoft at the Web Control level, and its functionality is exposed to you by way of properties (see Figure 1
|Figure 1. The GridView Control: The GridView control looks very similar to the DataGrid, but has much more built-in functionality.|
In ASP.NET 2.0, Microsoft will provide a few more controls to save developers some programming (both visually and code-wise) in areas where classically developers have had to build things manually. The DetailsView and FormsView WebControls provide other ways to display information using little or no code at all.
The DetailsView Control
The DetailsView Web Control is making its debut in Visual Studio 2005 and offers another way to look at a collection of data. As the grids show tabular data with records represented in rows and record details represented in columns, the DetailsView shows one record at a time with the fields displaying vertically and their corresponding values displaying next to them (see Figure 2
). The data binding intelligence built into this control is the same as that of the GridView (or the DataGrid for that matter) control; the display is what's different. As with many other WebControls you've used in the past, extensive styling is provided in this and all the other controls. I'll talk a little more about styling later because it is a key issue when it comes to providing reusability in custom Web Controls.
The FormView Control
|Figure 2. The Details View Control: The DetailsView control displays one record at a time.|
The FormView Web Control, also new in Visual Studio 2005, displays data in a similar fashion to the DetailsView Web Control but it also offers the option of displaying data as an actual data-entry form. I mentioned earlier that Microsoft has given developers ways to do things that they had to do manually before; well, this is a perfect example. More often than not, most developers have programmed data-entry forms by building the WebForm out of labels, textboxes, buttons, and other controls they need in order to display and edit data (see Figure 3
The FormView Web Control allows you to do this through declarative programming by simply binding your control to a data source. The intelligence programmed into the FormView control will build a data-entry form for you and display it along with other ancillary visual aides as well, such as paging links and headings. Other properties in this control let you reuse the control in other situations.
For example, setting the DefaultMode property to ReadOnly changes your form to a view-only style where there are no textboxes, and instead data is displayed in labels. Think about how much work would be required to accomplish this in the traditional manner by building the WebForm manually.
|Figure 3. The FormView Control: The FormView control builds an entire data-editing form based on its data source.|
Web Controls that provide you with the ability to program declaratively in ASP.NET do not have to perform visual rendering. Visual Studio 2005 comes with a series of DataSource Web Controls that allow developers to define data queries. Instead of using code to open database connections, create commands, and execute SQL queries returning DataSets or DataReaders, the SqlDataSource control allows you to define the database connection and query in control properties. The control can then be bound to any of the above mentioned new controls or to any custom Web Controls that you may write.
This database information and the binding to other controls can be performed with no coding whatsoevera perfect example of declarative programming. Visual Studio 2005 provides other DataSource Web Controls, including one that lets you bind to business objects.
There are several other new Web Controls in Visual Studio 2005 that are designed to save developers a lot of visual design and coding time. The Login control renders a group of labels and textboxes that make up a generic login form. The ChangePassword control does the same but with a form that gives you the visual objects and functionality you would typically use to change a user's password. Web Controls such as these, which render what appear to be entire forms or a grouping of related data-entry controls, are new to Visual Studio 2005, but the ability to create them has been around since ASP.NET 1.0.