Adding Checkbox in asp.net Listview control to allow multiple delete
I am trying to display checkboxes in front of every row in list view. So that after selecting the desired checkboxes user clicks on delete button and we should delete that records.
bu开发者_开发技巧t how can it be done?
Add Checkbox in markup
<asp:CheckBox ID="ChkSelect" runat="server" />
code behind as follows:
Dim ChkSelect As CheckBox = Nothing
Dim ListItem As ListViewDataItem = Nothing
Dim ItemList As List(Of Person) = New List(Of Person)
Dim Item As Person= Nothing
For Each ListItem In MyDataList.Items
ChkSelect = ListItem.FindControl("ChkSelect")
If ChkSelect.Checked Then
Dim UIN As Integer = _
MyDataList.DataKeys(ListItem.DisplayIndex).Value.ToString()
Item = Persons.GetData(UIN)
Item.Deleted = True
ItemList.Add(Item)
End If
Next
Data = Persons.UpdateBulk(ItemList)
If Data = True Then
BindMyData()
End If
You need to create a template for the items in the ListView, put the checkbox in it, and then get all the items that were checked when you click the Delete button. You could either keep track of the selected items on the client or the server, but it would always require some work to persist them.
Here's an article on using templates with the ListView: http://msdn.microsoft.com/en-us/library/bb398790.aspx#CreatingTemplatesForTheListViewControl
I use GridView Template if I want to do that in GridView...try to look if theres ListView Template if there is.
精彩评论