All of the examples you have seen so far use the GridViewcontrol. This is, however, just one of the controls available for displaying the data exposed by a data source control. The GridViewcontrol, as you have seen, displays a grid of rows and columns, giving you a view of the data that corresponds directly to the original data table. You can change this by using different column types, or with templates, but the underlying structure of the output is still rows and columns, with one row for each row in the source data. Two of the other new data display controls in ASP.NET 2.0 display the data one row at a time, with the individual columns laid out like a “form.” They provide links that allow you to move from one page or row to the next one, the previous one, the first one, or the last one. This form-based view of the data often makes it easier to see what a row that has a large number of columns contains. The individual values can be displayed with separate labels for each one, laid out vertically as fields from one row rather than as a series of horizontally arranged columns.
The two controls that provide this type of output are the DetailsViewand FormView. The difference between them is that the DetailsViewcontrol, like the GridViewcontrol, can automatically generate the fields and display the content from the associated data source control. The FormViewcontrol, on the other hand, provides no automatic display support other than the navigation between rows, and you must create the entire content of the display form using templates. However, these templates work in just the same way as you saw for the GridViewcontrol in the previous section.
Other display controls for rowset data are the DataListand Repeater. The DataListand Repeater controls do not support automatic row selection, editing, paging, or sorting. The DataListcontrol generates an HTML table with one row containing a single cell for each row in the source data. You pro- vide details of the data items from the source data, and how they are to be displayed, for each row. The
Repeatercontrol is even simpler. It generates repeated output using a template that you create, without
providing any other support for formatting or layout. All it does is output to the browser the contents of the template once for each row in the data source.
1.
In the TestMenu2.aspxpage you used in the previous example, click on the GridViewcontrol and press the Delete key to remove it from the page. Then drag a DetailsViewcontrol from the Data section of the Toolbox and drop it onto the page just below the existing SqlDataSource control. In the DetailsView Tasks pane, select the existing data source (named SqlDataSource1) in the Choose Data Source drop-down list (see Figure 4-23).Figure 4-23: Selecting the existing data source
2.
Now click the Auto Format link in the DetailsView Tasks pane, and select one of the formats that are available from the list in the Auto Format dialog that appears. Then, back in the DetailsView Tasks pane, tick Enable Paging and you will see the navigation links appear below the control (see Figure 4-24).3.
Now drag a FormViewcontrol from the Data section of the Toolbox onto the page, placing it belowthe DetailsViewcontrol. Repeat the preceding steps to specify the existing SqlDataSourceas
the data source for the FormViewcontrol (you can bind more than one control to a single data source), apply an auto-format to it, and enable paging. The FormViewcontrol looks quite different from the DetailsViewcontrol (see Figure 4-25).
Figure 4-25: FormViewcontrol
In fact, VWD has added a lot of content to the FormViewcontrol for you. This allows it to be used straight away, without requiring you to edit and populate the templates. If you switch to Source
view, you will see that the DetailsViewcontains a series of BoundFieldcontrols, just as you saw used with the GridViewcontrol. For the FormViewcontrol, VWD has created an ItemTemplate, EditItemTemplate, and InsertItemTemplate section, and populated these with controls to display all the columns, and even allow editing of all the columns (with the exception of the primary key column) in the source data.
4.
Run the page, and again you see the differences between the two controls. The DetailsView control at the top displays the contents using an HTML table to lay out the captions and values, and adds row-level formatting. The FormViewcontrol simply contains the text generated by the default templates that VWD created (see Figure 4-26).Figure 4-26: DetailsViewcontrol and FormViewcontrol with text generated by the default templates
5.
Back in Design view, open the DetailsView Tasks pane, and click the Edit Fields link. You see the same Fields dialog as you did with the GridViewcontrol. You can add, remove, format, and change the types of the columns in the DetailsViewcontrol in just the same way as you saw earlier in this chapter with the GridViewcontrol. However, there is no Edit Fields link in the FormView Tasks pane, because it does not support the field controls. Instead, you must switch to template editing mode where you add, remove, change, and format the controls in each of the templates, just as you did in the previous example with a TemplateField(see Figure 4-27). The only difference is that, in the FormViewcontrol, the templates make up the entire content of the output, whereas a TemplateFieldin a GridViewcontrol just specifies the appearance for that column.Figure 4-27: Switching to template editing mode
The DetailsViewand FormViewcontrols in this example do not support editing or inserting new rows
because they are bound to a query that does not allow updates to be pushed back into the database. Recall that the query used by the SqlDataSourcecontrol in this page joins two tables and returns rows from both. If you use a query that returns rows from a single table, or from an updateable database view, the Tasks panes for the DetailsViewand FormViewcontrols will allow you to turn on the editing and inserting of new rows, and will automatically handle the whole process for you just like the GridView control does.
Summar y
This chapter has concentrated on the techniques available within VWD for creating pages that display data from a relational database. It is often important to know a little more about how a technology works to be able to get the best from it, and ASP.NET and VWD are certainly no exception. This chapter explored the basics of the ASP.NET event-driven architecture, and the way that this provides the ability to react to events in your own pages and code.
Equally important, for developers working with relational or XML data (which includes the vast majority of developers at some time or another), is a basic grasp of what the data source controls in ASP.NET actually do. You saw some details in this chapter, and you will see more in subsequent chapters. However, the bulk of the chapter was concerned with the use of templates in the data display controls such as the GridView. You tend to discover, surprisingly quickly, that the standard output generated by the data display controls in ASP.NET often does not give you exactly what you want. Instead, you