Link to home
Start Free TrialLog in
Avatar of TonyReba
TonyRebaFlag for United States of America

asked on

Maintaining Checkbox State in a Listview Pagination ASP.NET

I Have a ListView control on a asp.net web form. I am trying to make where I can paginate and the selected state of the checkbox is preserved so I can page trhough records and pagination and send them to a print page which takes those ids...

Can you please help on how to do this?

   
Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand

        Dim myButtonPrint1 As Button = CType(ListView1.FindControl("printButton1"), Button)
        If e.CommandSource Is myButtonPrint1 Then
            Dim recordsId As New List(Of String)
            For Each lvi1 As ListViewItem In ListView1.Items
                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"))

                If chb1.Checked = True Then
                    Dim param1 As String
                    param1 = DirectCast(lvi1.FindControl("lblId1"), Label).Text
                    recordsId.Add(param1)

                End If
            Next

            ' Store in session to be pulled out in the Printable Page
            Session("Records") = recordsId
            Response.Redirect("PrintableProviderList.aspx")
        End If
    End Sub


Private ReadOnly Property IDs() As List(Of Integer)
        ' Create a list of ID's that are selected.  ID's is the primary
        ' Key for this table
        Get
            If Me.ViewState("IDs") Is Nothing Then
                Me.ViewState("IDs") = New List(Of Integer)()
            End If
            Return CType(Me.ViewState("IDs"), List(Of Integer))
        End Get
    End Property

    Protected Sub AddRowstoIDList()
        ' Loop through all the current items in the Listview
        For Each lvi As ListViewDataItem In ListView1.Items
            ' Find the checkbox in each row
            Dim chkSelect As CheckBox = CType(lvi.FindControl("CheckBoxPrintProvider1"), CheckBox)
            ' If the checkbox is ticked then add the corresponding ID to our private
            ' list
            If (Not (chkSelect) Is Nothing) Then
                ' Get the ID from the datakeynames property
                Dim ID As Integer = Convert.ToInt32(ListView1.DataKeys(lvi.DisplayIndex).Value)
                If (chkSelect.Checked AndAlso Not Me.IDs.Contains(ID)) Then
                    ' Add the ID to our list
                    Me.IDs.Add(ID)
                ElseIf (Not chkSelect.Checked AndAlso Me.IDs.Contains(ID)) Then
                    ' Not checked - remove the ID from our list
                    Me.IDs.Remove(ID)
                End If
            End If
        Next
    End Sub



    Protected Sub ListView1_PagePropertiesChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.PagePropertiesChangingEventArgs) Handles ListView1.PagePropertiesChanging
        AddRowstoIDList()
    End Sub

    Protected Sub ListView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewSortEventArgs) Handles ListView1.Sorting
        AddRowstoIDList()
    End Sub


    Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView1.ItemDataBound
        ' Get each Listview Item on DataBound
        Dim lvi As ListViewDataItem = e.Item
        If (lvi.ItemType = ListViewItemType.DataItem) Then
            ' Find the checkbox in the current row
            Dim chkSelect As CheckBox = CType(lvi.FindControl("CheckBoxPrintProvider1"), CheckBox)
            ' Make sure we're referencing the correct control
            If (Not (chkSelect) Is Nothing) Then
                ' If the ID exists in our list then check the checkbox
                Dim ID As Integer = Convert.ToInt32(ListView1.DataKeys(lvi.DisplayIndex).Value)
                chkSelect.Checked = Me.IDs.Contains(ID)
            End If
        End If
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial