As you ve seen, the DataList control offers lots (Web server info)
Monday, October 22nd, 2007As you ve seen, the DataList control offers lots of different ways to display data from database tables. The template you define is applied to every record that s retrieve from the database table. But sometimes, you don t want to show data from multiple records. Sometimes, you just want to show a single record, or a single piece of information from a table. For those occasions, the FormView control might be your best bet. The FormView Control The FormView control is similar to the DetailsView control in that it only shows one record at a time. But it s also similar to a DataList control in that it displays data in such a way that it looks like text printed on a page rather than a table or form. If you want to display and edit one record at a time, you re much better off using the DetailsView control, as opposed to FormView. The DetailsView control, which is new in ASP.NET 2.0, is much easier to work with. The FormView control works like the other Data controls in that you drag it to a page, define your data source, and optionally edit its template to get the exact look you want. Because it s limited to displaying a single record, FormView can be useful for displaying a single item of data, such as a sum or total. Here s an example. In Chapter 11, I created a view named DetailedUsersItemsView which contains detailed information about every transaction. Suppose you want to sum up the ItemPrice field in that view to see a grand total of all transactions. You d start by dragging a FormView control onto a page. From its Common Tasks menu, choose New Data Source, choose Database, and click OK. As always, choose the standard connection string, and click Next. On the Configure the Select Statement page, you won t be able to use the Specify columns from a table or view in this example because you need a sum of all the numbers in a view. You have to choose Specify a custom SQL statement or stored procedure, then click Next to get to the more advanced query editor where you can specify a sum. On the next page that appears, click the Query Builder button. In the Add Table dialog box that opens, click the Views tab, click Detailed UsersItemsView, click Add, then click the Close button in the Add Table dialog box. In the Query Builder, scroll down the field list for the view and choose ItemPrice. Right-click some empty space in the top page of the Query Builder and choose Add Group By. Then, in the query grid, set the Group By column for the ItemPrice field to Sum, as shown in Figure 12-61. 312 Part III: Personalization and Databases
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.