“CheckAll”复选框仅适用于asp:Repeater的第一页。怎么修?

时间:2014-08-25 10:03:04

标签: c# javascript asp.net

我有一个带CheckBoxes的Repeater:

<asp:Repeater ID="rpt_users" runat="server" OnItemCommand="rpt_users_ItemCommand" OnItemDataBound="rpt_users_ItemDataBound">
    <HeaderTemplate>
        <table id="usersTable">
            <tr>
                <th rowspan="2">All<br /><asp:CheckBox runat="server" ID="checkAll" OnCheckedChanged="checkAll_CheckedChanged"/></th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr class="c0">
            <td><asp:CheckBox ID="CheckSelect" runat="server" /></td>               
        </tr>
    </ItemTemplate>
</asp:Repeater>

这是我在Repeater控制之后立即放置的脚本:

<script type="text/javascript">
    var repeater1Control = document.getElementById('<%= rpt_users.ClientID %>');
    $('input:checkbox[id$=checkAll]', repeater1Control).click(function (e) {
        if (this.checked) {
            $('input:checkbox[id$=CheckSelect]', repeater1Control).attr('checked', true);
        }
        else {
            $('input:checkbox[id$=CheckSelect]', repeater1Control).removeAttr('checked');
        }
    });



    $('input:checkbox[id$=CheckSelect]', repeater1Control).click(function (e) {
        //To uncheck the header checkbox when there are no selected checkboxes in itemtemplate
        if ($('input:checkbox[id$=CheckSelect]:checked', repeater1Control).length == 0) {
            $('input:checkbox[id$=checkAll]', repeater1Control).removeAttr('checked');
        }
            //To check the header checkbox when there are all selected checkboxes in itemtemplate
        else if ($('input:checkbox[id$=CheckSelect]:checked', repeater1Control).length == $('input:checkbox[id$=CheckSelect]', repeater1Control).length) {
            $('input:checkbox[id$=checkAll]', repeater1Control).attr('checked', true);
        }
    });

不幸的是,它仅适用于转发器的第一页。如果我进入第二页或更进一步,当我在标题中按“CheckAll”复选框时没有任何反应。如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

试试这个

(function($){ 

  var bindEvents =  function(){
   // bind events here;
    var repeater1Control = document.getElementById('<%= rpt_users.ClientID %>');
    $('input:checkbox[id$=checkAll]', repeater1Control).click(function (e) {
        if (this.checked) {
            $('input:checkbox[id$=CheckSelect]', repeater1Control).attr('checked', true);
        }
        else {
            $('input:checkbox[id$=CheckSelect]', repeater1Control).removeAttr('checked');
        }
    });



    $('input:checkbox[id$=CheckSelect]', repeater1Control).click(function (e) {
        //To uncheck the header checkbox when there are no selected checkboxes in itemtemplate
        if ($('input:checkbox[id$=CheckSelect]:checked', repeater1Control).length == 0) {
            $('input:checkbox[id$=checkAll]', repeater1Control).removeAttr('checked');
        }
            //To check the header checkbox when there are all selected checkboxes in itemtemplate
        else if ($('input:checkbox[id$=CheckSelect]:checked', repeater1Control).length == $('input:checkbox[id$=CheckSelect]', repeater1Control).length) {
            $('input:checkbox[id$=checkAll]', repeater1Control).attr('checked', true);
        }
    });
  };

  // initial load
  $(document).ready( bindEvents);

  // every async load by update panel
  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(bindEvents);

})(jQuery);