Stone Stone - 2 years ago 207 Question

Select specific rows from ListView selected with checkboxes

I have placed ListView in my page with checkbox on each rows. Now suppose I have 10 records in listview & from 10 I want to check 4 so which ever is checked ité ID should get stored in string with (,) so I can able to update my table. Please tell me how can I do it. Since I am new to I tried something from tutorial but it's not working..


Private Sub assignOrder_Click(sender As Object, e As EventArgs) Handles assignOrder.Click
For Each item As ListViewDataItem In Me.medicalList.Items
If item.ItemType = ListViewItemType.DataItem Then
Dim checkedChemist As CheckBox = TryCast(item.FindControl("selectChemists"), CheckBox)
If checkedChemist.Checked Then
Dim constr As String = ConfigurationManager.ConnectionStrings("conio2").ConnectionString
Using con2 As New MySqlConnection(constr)
Using cmd As New MySqlCommand("Update newMedicinesOrders set `status` = @status, `viewBy` = '" + +"' WHERE OrderID = @orderID")
Using sda As New MySqlDataAdapter()
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@orderID", orderid.Text)
cmd.Parameters.AddWithValue("@status", "Pending")
cmd.Connection = con2
End Using
End Using
End Using
End If
End If
End Sub


<asp:ListView ID="medicalList" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
<table class="pharmaList">
<td class="header-row"></td>
<td class="header-row">ID</td>
<td class="header-row">Name</td>
<td class="header-row">Email</td>
<td class="header-row">Contact Number</td>

<asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
<td colspan="9">
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="medicalList" PageSize="10">
<asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
ShowNextPageButton="false" />
<asp:NumericPagerField ButtonType="Link" />
<asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />

<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>

<td><asp:CheckBox ID="selectChemists" runat="server" /></td>
<td class="content-row"><asp:Label ID="id" runat="server" Text='<%# Eval("chemistID") %>'></asp:Label></td>
<td class="content-row"><asp:Label ID="chemistName" runat="server" Text='<%# Eval("name") %>'></asp:Label></td>
<td class="content-row"><asp:Label ID="chemistEmail" runat="server" Text='<%# Eval("email") %>'></asp:Label></td>
<td class="content-row"><asp:Label ID="chemistContact" runat="server" Text='<%# Eval("mobileNumber") %>'></asp:Label></td>

<div class="form-row">
<strong>No Medicals Found...</strong>

Answer Source

I would loop through your listview by finding all of the checked items. Once you have found your items you could then put them in a list or for each call your SQL. If you looped for each to sql you will be calling the database each time. In either case try to get the items checked first.

Below will loop through you listview and get all checked items and place them into a String.

Dim MyItems as String = ""
For Each item in medicalList.CheckedItems
MyItems += item.Text
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download