Browse DevX
Sign up for e-mail newsletters from DevX


Using the New GridView Control in ASP.NET 2.0 : Page 4

Of all the improvements made in the release of ASP.NET 2.0, the most important by far is the ability to let the controls perform most of the work for you. Learn how declaratively configuring the new GridView control allows you to get more work done with less code!




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 exception—it 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:

<asp:SqlDataSource ID="SqlDataSource1" Runat="server" 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) WHERE (ProductID=@ProductID)" DeleteCommand="DELETE FROM Products WHERE ProductID=@ProductID" InsertCommand="INSERT INTO Products (ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice) VALUES (@ProductName, @SupplierID, @CategoryID, @QuantityPerUnit,CONVERT(money,@UnitPrice))"> </asp:SqlDataSource>

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).

Codeless Controls
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.

Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Thanks for your registration, follow us on our social networks to keep up-to-date