2012-03-19 76 views
0

維護複選框狀態我有一個asp.net web表單3.5的Web表單。列表視圖在項目模板上有一個複選框,我試圖通過分頁保留複選框的狀態。一旦我可以通過記錄和保存這個狀態的頁面,我需要將它發送到一個打印頁面,它接受這些ID ...我有這個工作,但它只打印每個分頁上的記錄。請看直播網站: http://rgvpreferred.com/ProviderSearch.aspxListView控件分頁ASP.NET

你能檢查我的代碼,並建議如何做到這一點,下面的代碼是行不通的。

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 

回答

0

您必須在分頁之前保存所選的複選框。 看到這個鏈接。它應該幫助你: http://evonet.com.au/maintaining-checkbox-state-in-a-listview/

+0

可以請你看看我的代碼?我試圖simmilar但它不會工作, – rgvwed 2012-03-19 18:41:19

+0

你在哪裏在打印功能拍攝的ID列表(從視圖狀態)的東西嗎?我沒有看到您將列表發送到可打印頁面 – 2012-03-20 07:16:51

+0

您是否發現問題? – 2012-03-21 11:35:38