Solved

Maintaining Checkbox State in a Listview Pagination ASP.NET

Posted on 2012-03-16
3
964 Views
Last Modified: 2012-03-24
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

0
Comment
Question by:TonyReba
3 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now