2011-02-11 61 views
0

我想採取C#和VB.NET的建議。計數所有選中CheckBox的ListView中有分頁

我有DataPager的一個簡單的ListView類似如下:

<asp:ListView ID="lvStudent" runat="server"> 
    <LayoutTemplate> 
     <table id="TimeSheet" cellspacing="1" class="tablesorter"> 
      <thead> 
       <tr> 

       <th> 
       ID# 
       </th> 

        <th> 
         <asp:LinkButton ID="lnkByName" runat="server">Name</asp:LinkButton> 
        </th> 
        <th> 
         <asp:CheckBox ID="selectAll" runat="server" OnClick="selectAll(this)"/> 
        </th>   

       </tr> 
      </thead> 
      <tbody> 
       <tr id="itemPlaceholder" runat="server" /> 
      </tbody> 
     </table> 
    </LayoutTemplate> 
    <ItemTemplate> 
     <tr> 
     <td class="id"> 
     <%#Eval("Id")%> 
     </td> 

      <td> 
       <%#Eval("Name")%> 
       <asp:HiddenField ID="hfId" runat="server" Value='<%#Eval("Id")%>' /> 
      </td> 

      <td> 
       <asp:CheckBox ID="cbSelected" runat="server"/> 
       </td>    
    </ItemTemplate> 
</asp:ListView> 


<div> 
    <asp:DataPager ID="dpReport" runat="server" 
    PagedControlID="lvStudent" PageSize="20"> 
    <Fields> 
     <asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="False" /> 
     <asp:NumericPagerField NumericButtonCssClass="dpTimeSheet" ButtonCount="50" ButtonType="Link" /> 
     <asp:NextPreviousPagerField ShowLastPageButton="True" ShowPreviousPageButton="False" /> 
    </Fields> 
</asp:DataPager> 
</div> 

如果沒有分頁,我可以得到所有選中的複選框這樣的:

Public Function CountSelectedCheckBox() As Integer 

    Dim count As Integer = 0 
    Dim s As String 

    For Each i As ListViewDataItem In lvStudent.Items 

     Dim cb = CType(i.FindControl("cbSelected"), CheckBox) 

     If cb.Checked Then 
      count = count + 1 
     End If 
    Next 

    Return count 

End Function 

但是,我怎麼算都檢查ListView中有分頁的CheckBox?

回答

0

我已經實現了類似於你的功能的東西。 嘗試在會話或ViewState中保存checkedIds。 當然,您需要爲每個請求更新checkedIds。

相關問題