Fun Pet Trick 11: Data Binding to Generics


Fun Pet Trick #11: Data Binding to Generics

By Scott Guthrie

One of the great new features in Whidbey is Generics, which basically provide a mechanism that enables developers to build classes whose signature and internal data types can be templatized.

For example, rather than use an ArrayList (which is a collection of type Object), or force developers to create their own strongly typed list collection class (such as the OrderCollection class), developers using Whidbey can employ the new List class implemented within the System.Collections.Generic namespace, and specify the type of the collection when using or referencing it.

For example:

 // Use the built-in "List" collection within // the System.Collections.Generic namespace // to create a collection of type "Order" List<Order> orders = new List<Order>(); // Add Order objects into the list orders.Add(new Order(123, "Dell")); orders.Add(new Order(345, "Toshiba")); orders.Add(new Order(567, "Compaq")); // Lookup the "OrderId" of the first item in the list - // note that there is no cast below, // because the collection items are each an "Order" object // (as opposed to "Object" // which they would be with an ArrayList int orderId = orders[0].OrderId; // The following statement will generate a compile error, but would have // compiled (but generated a runtime exception) if the collection was // an ArrayList orders.Add("This will not work because it isn't an order object"); 

The following code is a richer example of how to use Generics with the new ASP.NET ObjectDataSource control, and then bind the list to a GridView control.

First is the OrderSystem.cs file, which should be saved within the code directory immediately underneath the application root:

 // OrderSystem.cs: Save within "code" directory using System; using System.Collections.Generic; public class Order {     private int _orderId;     private string _productName;     public Order(int orderId, string productName)     {        _orderId = orderId;        _productName = productName;     }     public string ProductName     {         get         {             return _productName;          }     }     public int OrderId     {         get         {            return _orderId;         }     } } public class OrderSystem {    public List<Order> GetOrders()    {        List<Order> orders = new List<Order>();        orders.Add(new Order(123, "Dell"));        orders.Add(new Order(345, "Toshiba"));        orders.Add(new Order(567, "Compaq"));        return orders;    } } 

I can then write a simple ASPX page that uses the ObjectDataSource control to bind against the GetOrders() method to retrieve a list of Order objects. I can then point the GridView at the ObjectDataSource control:

 <%@ page language="C#" %> <html> <body>     <form runat="server">         <asp:gridview  runat="server"             datasource bordercolor="#CC9966"             borderstyle="None" borderwidth="1px" backcolor="White" cellpadding="4">             <headerstyle forecolor="#FFFFCC" backcolor="#990000"                 font-italic="False" font-bold="True">             </headerstyle>         </asp:gridview>         <asp:objectdatasource  runat="server"             typename="OrderSystem"             selectmethod="GetOrders">         </asp:objectdatasource>     </form> </body> </html> 

Enjoy ...




ASP. NET 2.0 Revealed
ASP.NET 2.0 Revealed
ISBN: 1590593375
EAN: 2147483647
Year: 2005
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net