Binding to a Class

In the previous example, you bound an ArrayList of strings to the list box. Often, you will want to bind objects more complex than strings. For example, you might imagine a Book class that has properties such as title, ISBN, and price, as shown in Example 9-3 (C#) and Example 9-4 (VB.NET).

Example 9-3. The Book class in C#

public class Book
{
   public Book(float price, string title, string ISBN)
   {
      this.price = price;
      this.title = title;
      this.isbn = ISBN;
   }

   public float Price { get {return price;} }
   public string Title { get {return title;} }
   public string ISBN { get {return isbn;} }

   private float price;
   private string title;
   private string isbn;

}

Example 9-4. The Book class in VB.NET

Public Class Book

   Private _Price As Double
   Private _Title As String
   Private _ISBN As String

   Public Sub New( _
       ByVal thePrice As Double, _
       ByVal theTitle As String, _
       ByVal theISBN as string)
      _Price = thePrice
      _Title = theTitle
      _ISBN = theISBN
   End Sub

   Public ReadOnly Property Price(  ) As Double
      Get
         Return _Price
      End Get
   End Property

   Public ReadOnly Property Title(  ) As String
      Get
         Return _Title
      End Get
   End Property

   Public ReadOnly Property ISBN(  ) As String
      Get
         Return _ISBN
      End Get
   End Property

End Class

You add the new book objects to the ArrayList, just as you assigned the strings, shown here in C#. (The VB.NET code is the same except without the semicolons.)

bookList.Add(new Book(49.95f, "Programming ASP.NET","100000000"));
bookList.Add(new Book(49.95f,"Programming C#","0596001177"));
bookList.Add(new Book(34.99f,"Teach Yourself C++ In 21 
             Days","067232072x"));
bookList.Add(new Book(24.95f,"Teach Yourself C++ In 24 
              Hours","0672315165"));
bookList.Add(new Book(12.99f,"TY C++ In 10 Minutes","067231603X"));

If you are going to bind a collection of Book objects to the list box, you must tell the list box which property to bind to, so that it will know which property to display. You do this by adding the following lines indicated in boldface to the page’s HTML source:

<asp:DropDownList 
ID="ddlBooks" 
autopostback="True"
DataTextField="Title"
DataValueField="ISBN"
Runat="server"></asp:DropDownList>

The DataTextField attribute assigns the property that will be displayed in the list box, while the DataValueField attribute assigns the property that will be held in the value attribute of the list box item. When you run this and examine the source through the browser, you find that ASP.NET has translated the ASP controls to the appropriate HTML as follows:

<option value="100000000">Programming ASP.NET</option>
<option value="100000000">Programming C#</option>
<option value="067232072x">Teach Yourself C++ In 21 Days</option>

Change the label to display the ISBN along with the title:

lblMsg.Text = "Selected: " + ddlBooks.SelectedItem.Text +
   "(" + ddlBooks.SelectedItem.Value + ")";

or in VB:

lblMsg.Text = "Selected book: " & ddlBooks(  ).SelectedItem.Text _
& " (" & ddlBooks(  ).SelectedItem.Value & ")"

The result is that the user selects a book and the book title and ISBN are displayed, as shown in Figure 9-2.

Binding an array of objects

Figure 9-2. Binding an array of objects

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.57.172