Populating the ComboBox Control
The sample page described in this section (populating.aspx) demonstrates different ways of populating the ComboBox user control. As in the previous example, it registers the control with a Register directive and then declares three instances of it. This time, they are all of the default style. However, the lists are filled using three different techniques this time, as you can see in Figure 5.12.
Figure 5.12 Filling the ComboBox control list, using different data sources and data binding.
The first list is filled using the same ArrayList instance as in the previous example. The second is filled from the Northwind sample database that is supplied with SQL Server, using the values from the ProductName column of the Products table. The third ComboBox control is filled by creating new ListItem instances in code and adding them to the ComboBox control's Items collection (the ListItemCollection instance exposed by the Items property). This section of code also demonstrates how you can access a specific item in the list and read or change its value.
Editing the Connection String
You must edit the connection string in the web.config file to point to your database server, and you must specify the correct username and password if you run the examples on your own server.
All this is done within the Page_Load event handler for the sample page, with the exception of a separate routine that creates a DataReader instance for the table in the Northwind database. Listing 5.31 shows the complete code for this sample page.
Listing 5.31Code That Demonstrates Techniques for Populating the ComboBox Control
Sub Page_Load() If Not Page.IsPostback Then ' populate combobox controls ' databind ArrayList to first one Dim aVals As New ArrayList() aVals.Add("aardvark") aVals.Add("baboon") aVals.Add("buffalo") aVals.Add("cheetah") aVals.Add("frog") aVals.Add("giraffe") aVals.Add("lion") aVals.Add("lynx") cboTest1.DataSource = aVals ' databind second combobox to a DataReader cboTest2.DataSource = GetDataReader() cboTest2.DataTextField = "ProductName" ' insert values directly into list for third combobox Dim oList As ListItemCollection = cboTest3.Items For iLoop As Integer = 1 To 9 oList.Add(New ListItem("Item: " & iLoop.ToString())) Next oList.Insert(3, New ListItem("Inserted: 3a")) End If End Sub Function GetDataReader() As OleDbDataReader ' get DataReader for rows from Northwind Products table Dim sConnect As String _ = ConfigurationSettings.AppSettings("NorthwindOleDbConnectString") Dim sSelect As String _ = "SELECT ProductName FROM Products WHERE ProductName LIKE 'c%'" Dim oConnect As New OleDbConnection(sConnect) Try oConnect.Open() Dim oCommand As New OleDbCommand(sSelect, oConnect) Return oCommand.ExecuteReader(CommandBehavior.CloseConnection) Catch oErr As Exception ' be sure to close connection if error occurs If oConnect.State <> ConnectionState.Closed Then oConnect.Close() End If ' display error message in page lblErr.Text = oErr.Message End Try End Function