Stone Stone - 5 months ago 58
Vb.net 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 vb.net I tried something from tutorial but it's not working..

vb

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
con2.Open()
cmd.ExecuteReader()
con2.Close()
End Using
End Using
End Using
Response.Redirect(Request.Url.AbsoluteUri)
End If
End If
Next
End Sub


aspx

<asp:ListView ID="medicalList" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
<LayoutTemplate>
<table class="pharmaList">
<tr>
<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>
</tr>

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

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

<ItemTemplate>
<tr>
<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>
</tr>
</ItemTemplate>

<EmptyDataTemplate>
<div class="form-row">
<strong>No Medicals Found...</strong>
</div>
</EmptyDataTemplate>
</asp:ListView>

Answer

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
MsgBox(item.Text)
MyItems += item.Text
Next
Comments