8.5 Work with Data-Bound Multi-Select List Boxes Using Web Forms
As with How-to 8.1, this example will show you how to take advantage of multi-select
list boxes, only with a Web Form instead of a Windows Form.
You need to be able to manipulate multi-select list boxes in your Web applications using
ASP.NET as well as in your Visual Basic .NET desktop applications. This How-To
shows you how to use just about the same coding techniques as in How-To 8.1, but with
the change of using the Web Form.
Technique
When you are performing a task in a Web Form that you have created in a Windows
Form, you would think it would take the same effort-if not more-to accomplish the task.
However, this is not the case for this How-To. The commands available to the Windows
Form ListBox control will give better performance because you have a SelectedIndexes
collection to work with, and in the Web Form you iterate through all the items in the
ListBox control and check the Selected property. Nonetheless, coding on the Web Form
is simpler.
Unlike the Windows Form version of the ListBox Control, which has four different
settings for the SelectionMode property, the Web Form version has two: Single or
Multiple.
Another thing to keep in mind when developing with data and the Web Form is that you
will need to use the DataBind method off the ListBox control to bind the data at runtime.
In the Load event of the page, you will want to use the IsPostBack method of the page to
ensure that you perform certain tasks only when the page is initially loaded, and not on a
round trip that pages take back to the server.
Steps
Open and run the VB.NET -Chapter 8 solution. From the main Web Form, click on the
hyperlink with the caption How-To 8.5: Work with Data-Bound Multi-Select List Boxes
Using a Web Form. You will then see the page displayed in Figure 8.8.
Figure 8.8. This Web Form uses controls bound at runtime and takes advantage of
multi-select list boxes.
When the page loads, you will see the Beverages category chosen in the top combo box.
Command Button Name btnSelect
Text >
Command Button Name btnUnSelect
Text <
CheckBox Name chkUnAssignedOnly
Label Name Label4
Text UnAssigned Products Only
HyperLink Name hplReturnToMain
Text Return To Main
NavigateURL wfrmMain.aspx
2. Note
HyperLink is optional. It is just used to get back to the main
sample page for this chapter.
3. As with some of the other chapters' projects, before creating the code that will be
attached to the Load event of the form, you need to build a support routine to
create the Connection string. Called BuildCnnStr, the function can be seen in
Listing 8.24. This function takes a server and database names that are passed to it
and creates a Connection string.
Listing 8.24 modGeneralRoutines.vb: Creating a Connection String
Function BuildCnnStr(ByVal strServer As String, _
ByVal strDatabase As String) As String
Dim strTemp As String
strTemp = "Provider=SQLOleDB; Data Source=" & strServer & ";"
strTemp &= "Initial Catalog=" & strDatabase & ";"
strTemp &= "Integrated Security=SSPI"
Return strTemp
End Function
" From Categories",
BuildCnnStr("(local)", "Northwind"))
odaCategories.Fill(dtCategories)
Me.ddCategories.DataSource = dtCategories
Me.ddCategories.DataTextField = "CategoryName"
Me.ddCategories.DataValueField = "CategoryID"
'-- This is necessary for Web Forms, but not used on Windows Forms.
Me.ddCategories.DataBind()
LoadUnSelectedProducts()
LoadSelectedProducts()
End If
End Sub
5. Create the LoadUnSelectedProducts routine, shown in Listing 8.26, by entering
the following code in the Web Form that you created for this How-To. This
routine starts off by testing the check box called chkUnAssignedOnly. Based on
that value, a SQL string is created that grabs the products that are either not
assigned to any product, if chkUnAssignedOnly = True, or all products that are not
assigned to the chosen category are retrieved. The SQL String is stored in the
variable called strSQL. Next, the DataAdapter object called odaUnselected is set