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

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

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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 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 ( Then strItems = strItems & ", " End If strItems = strItems & CType(oCurr.Value, String) End If Next ' Run an update query to assign the category to the desired products, ' using an IN clause in the SQL statement Try Dim ocnn As New OleDb.OleDbConnection(BuildCnnStr("(local)", _ "Northwind")) Dim ocmdSelect As New _ OleDb.OleDbCommand("Update Products Set CategoryID = " & Me.ddCategories.SelectedItem.Value & _ " Where ProductID IN (" & strItems & ")", ocnn) ocmdSelect.CommandType = CommandType.Text ocnn.Open() ocmdSelect.ExecuteNonQuery() Catch excpCommand As Exception End Try LoadUnSelectedProducts() LoadSelectedProducts() End Sub 10 Add the code in Listing 8.31 to the Click event of the btnUnSelect command button Again, this code is similar to the previous step, but it is used to set the CategoryID column to null if the product was highlighted in the lstSelected list box and btnUnSelect was clicked Listing 8.31 frmHowTo8_5.aspx: Updating the Server with Products That Are Unselected for the Given Category Private Sub btnUnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnSelect.Click Dim intItemsNum As Integer Dim strItems As String Dim oCurr As Object ' Iterate through each of the items in lstSelected ' and check the Selected property ' If selected, store into a string with other selected product IDs For Each oCurr In Me.lstSelected.Items If oCurr.Selected() = True Then If Len(strItems) > Then strItems = strItems & ", " End If strItems = strItems & CType(oCurr.Value, String) End If Next Try Dim ocnn As New OleDb.OleDbConnection(BuildCnnStr("(local)", _ "Northwind")) Dim ocmdUnSelect As New _ OleDb.OleDbCommand("Update Products Set CategoryID = Null " & _\\ "Where ProductID IN (" & _ strItems & ")", ocnn) ocmdUnSelect.CommandType = CommandType.Text ocnn.Open() ocmdUnSelect.ExecuteNonQuery() Catch excpCommand As Exception End Try LoadUnSelectedProducts() LoadSelectedProducts() End Sub How It Works When the user chooses a category, the appropriate items are loaded into the two list boxes Unselected items are in the list box on the left, and the selected items are in the list box on the right If the check box is selected, then those only those items that are not currently assigned to categories are displayed in the list box on the left, which is titled Unselected Products When the btnSelect button is clicked, any items that are highlighted in the lstUnSelected list box are used in a query that updates the server with the new category they now belong to Similarly, when the btnUnSelect is clicked, items that are listed in the lstSelected list box are used in a query that updates the CategoryID of the products to null Comments As mentioned in How-To 8.1, this example is not the smartest to create in real life because you want products to be assigned to a category However, this example does a good job of showing the properties and methods you can use to work with the multiselect features of the ListBox control ... How-To This routine basically performs the same tasks that the routine listed in the previous step does, except that it performs the tasks using the lstSelected ListBox control This routine also... How It Works When the user chooses a category, the appropriate items are loaded into the two list boxes Unselected items are in the list box on the left, and the selected items are in the list. .. 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

Ngày đăng: 24/12/2013, 06:17

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan