· 8 years ago · Jul 29, 2018, 03:02 PM
1Maintaining Checkbox State in a Listview Control Pagination ASP.NET
2Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand
3
4 Dim myButtonPrint1 As Button = CType(ListView1.FindControl("printButton1"), Button)
5 If e.CommandSource Is myButtonPrint1 Then
6 Dim recordsId As New List(Of String)
7 For Each lvi1 As ListViewItem In ListView1.Items
8 Dim chb1 As CheckBox = IIf(lvi1.FindControl("CheckBoxPrintProvider1") Is Nothing, IIf(lvi1.FindControl("CheckBoxPrintProvider3") Is Nothing, lvi1.FindControl("CheckBoxPrintProvider4"), lvi1.FindControl("CheckBoxPrintProvider3")), lvi1.FindControl("CheckBoxPrintProvider1"))
9
10 If chb1.Checked = True Then
11 Dim param1 As String
12 param1 = DirectCast(lvi1.FindControl("lblId1"), Label).Text
13 recordsId.Add(param1)
14
15 End If
16 Next
17
18 ' Store in session to be pulled out in the Printable Page
19 Session("Records") = recordsId
20 Response.Redirect("PrintableProviderList.aspx")
21 End If
22 End Sub
23
24'Trying to Preserve states
25
26Private ReadOnly Property IDs() As List(Of Integer)
27 ' Create a list of ID's that are selected. ID's is the primary
28 ' Key for this table
29 Get
30 If Me.ViewState("IDs") Is Nothing Then
31 Me.ViewState("IDs") = New List(Of Integer)()
32 End If
33 Return CType(Me.ViewState("IDs"), List(Of Integer))
34 End Get
35 End Property
36
37
38
39 Protected Sub AddRowstoIDList()
40 ' Loop through all the current items in the Listview
41 For Each lvi As ListViewDataItem In ListView1.Items
42 ' Find the checkbox in each row
43 Dim chkSelect As CheckBox = CType(lvi.FindControl("CheckBoxPrintProvider1"), CheckBox)
44 ' If the checkbox is ticked then add the corresponding ID to our private
45 ' list
46 If (Not (chkSelect) Is Nothing) Then
47 ' Get the ID from the datakeynames property
48 Dim ID As Integer = Convert.ToInt32(ListView1.DataKeys(lvi.DisplayIndex).Value)
49 If (chkSelect.Checked AndAlso Not Me.IDs.Contains(ID)) Then
50 ' Add the ID to our list
51 Me.IDs.Add(ID)
52 ElseIf (Not chkSelect.Checked AndAlso Me.IDs.Contains(ID)) Then
53 ' Not checked - remove the ID from our list
54 Me.IDs.Remove(ID)
55 End If
56 End If
57 Next
58 End Sub
59
60
61
62 Protected Sub ListView1_PagePropertiesChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.PagePropertiesChangingEventArgs) Handles ListView1.PagePropertiesChanging
63 AddRowstoIDList()
64 End Sub
65
66 Protected Sub ListView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewSortEventArgs) Handles ListView1.Sorting
67 AddRowstoIDList()
68 End Sub
69
70
71 Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView1.ItemDataBound
72 ' Get each Listview Item on DataBound
73 Dim lvi As ListViewDataItem = e.Item
74 If (lvi.ItemType = ListViewItemType.DataItem) Then
75 ' Find the checkbox in the current row
76 Dim chkSelect As CheckBox = CType(lvi.FindControl("CheckBoxPrintProvider1"), CheckBox)
77 ' Make sure we're referencing the correct control
78 If (Not (chkSelect) Is Nothing) Then
79 ' If the ID exists in our list then check the checkbox
80 Dim ID As Integer = Convert.ToInt32(ListView1.DataKeys(lvi.DisplayIndex).Value)
81 chkSelect.Checked = Me.IDs.Contains(ID)
82 End If
83 End If
84 End Sub