在Listview控件分页ASP.NET中维护复选框状态

时间:2012-03-19 03:18:00

标签: asp.net vb.net listview

我在asp.net网络表单3.5中有一个Web表单。列表视图在项目模板上有一个复选框,我试图通过分页保留复选框的状态。一旦我可以翻阅记录并保存这个状态,我需要将其发送到打印页面,该页面带有这些ID ...我有这个工作,但它只打印每个分页的记录。请看实时网站: http://rgvpreferred.com/ProviderSearch.aspx

请您查看我的代码并建议如何完成此操作,以下代码无效。

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

'Trying to Preserve states

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

1 个答案:

答案 0 :(得分:0)

您必须在分页前保存所选的复选框。 看到这个链接。它应该帮助你: http://evonet.com.au/maintaining-checkbox-state-in-a-listview/