9.3 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 (VB.NET) and Example 9-4 (C#).

Example 9-3. 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 price As Double, _        ByVal title As String, _        ByVal ISBN as string)       _Price = price       _Title = title       _ISBN = ISBN    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
Example 9-4. The Book class in C#
public class Book {    private float price;    private string title;    private string isbn;    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;} } }

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    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="0596001177">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 book: " & ddlBooks(  ).SelectedItem.Text _ & " (" & ddlBooks(  ).SelectedItem.Value & ")"

In C#, it looks like this:

lblMsg.Text = "Selected: " + 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.

Figure 9-2. Binding an array of objects
figs/pan2_0902.gif


Programming ASP. NET
Programming ASP.NET 3.5
ISBN: 0596529562
EAN: 2147483647
Year: 2003
Pages: 156

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