|Figure 10. Using the DetailsView Control: Bind the control to SqlDataSource1 and check all the checkboxes.|
The DetailsView Control
The DetailsView control is similar to the GridView control with one notable exceptionit displays one record at a time. To see how the DetailsView control works, drag-and-drop a DetailsView control onto the page. Bind it to SqlDataSource1 and check all the checkboxes shown in Figure 10.
Press F5 to load the page. You will be able to browse through all the records as well as edit or delete them.
Inserting a New Record
You've seen how to display, edit, and delete records using the GridView and DetailsView controls. While the Beta 1 version of GridView control doesn't support adding records, the DetailsView control does. To let users add new records, simply modify the source view of the SqlDataSource control by adding the InsertCommand attribute:
SelectCommand="SELECT [ProductID], [ProductName], [SupplierID], [CategoryID],
[QuantityPerUnit], [UnitPrice] FROM [Products]"
ConnectionString="<%$ ConnectionStrings:NorthWindConn %>"
UpdateCommand="UPDATE Products SET ProductName = @ProductName,
SupplierID= @SupplierID, CategoryID = @CategoryID ,
QuantityPerUnit = @QuantityPerUnit , UnitPrice = CONVERT(money,@UnitPrice)
DeleteCommand="DELETE FROM Products WHERE ProductID=@ProductID"
InsertCommand="INSERT INTO Products (ProductName, SupplierID, CategoryID,
QuantityPerUnit, UnitPrice) VALUES (@ProductName, @SupplierID, @CategoryID,
|Figure 11. Add a New Record: Add a new record by clicking on the New link on the DetailsView control.|
When this is done, the DetailsView control's Smart Tag will display the Enable Inserting checkbox. Check that checkbox to allow users to add a new record.
Add a new record by clicking on the New link on the DetailsView control (see Figure 11).
Using the new GridView and DetailsView controls in ASP.NET 2.0 have not required you to write a single line of code. You simply configure the controls declaratively, and the controls perform most of the work for you.