Tải bản đầy đủ (.pdf) (12 trang)

Tài liệu Work with Data-Bound Multi-Select List Boxes Using Web Forms docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (33.36 KB, 12 trang )

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.


The Selected and Unselected Products ListBox controls are filled in with the appropriate
products.
If you click on a product in the Unselected Products list box and then click on the arrow
button pointing to the right (>), the item is moved to the Selected Products list box. If you
select items in the Selected Products list box and click on the arrow button pointing to the
left (<), those items are moved to the Unselected Products list box.
If you click on the Unassigned Products Only check box at the bottom of the form, the
Unselected Products list box is filled with products that are not assigned to any category.
Note
If you check the Unassigned Products Only check box when you are first
getting into Northwind and running this example, you probably won't see
unassigned items. You will need to unselect products from a category.

1. Create a Web Form. Then place the controls shown in Figure 8.8 with the
properties set as seen in Table 8.7.
Table 8.7. Label, ComboBox, ListBox, and Command Button Control
Property Settings
Object Property Setting
DOCUMENT bgColor buttonface
Label Name Label1
Text Category:
DropDown Name ddCategories
Label Name Label2
Text Unselected Products
ListBox Name lstUnSelected
SelectionMode Multiple
Label Name Label3
Text Selected Products
ListBox Name lstSelected
SelectionMode Multiple

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

Although you could create a routine that would pass back a Connection object, a
more versatile method would be to pass back a string. Some objects ask you for a
Connection object, but others just want a string. You will see BuildCnnStr called
in the next step.
4. On the form, add the code in Listing 8.25 to the Load event. In this code, the first
task is to make sure the code is run only once in the page, when it is first loading.
The Not IsPostBack check performs this task. Next, you will create a data adapter
called odaCategories and load the categories SQL Statement into it. The
dtCategories data table is filled and set as the DataSource property of
ddCategories. The DataTextField and DataValueField of ddCategories are then
set. After that, the DataBind method of the DropDown is called. This is necessary
for binding data to the server controls on Web Forms. Finally, two new
subroutines called LoadUnSelectedProducts and LoadSelectedProducts are called
to populate the appropriate list boxes. These routines are discussed in the next two
steps.
Listing 8.25 wfrmHowTo8_5.aspx: Loading Categories into a List Box
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

Dim odaCategories As OleDb.OleDbDataAdapter
Dim dtCategories As New DataTable()

'-- Make sure this code is only executed when the page is first loaded.
If Not Page.IsPostBack Then

'-- Load up the Categories DropDown control

odaCategories = New _
OleDb.OleDbDataAdapter("Select CategoryID, CategoryName " & _

" 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

×