简体   繁体   中英

How to get GridView checkAll checkbox to not fire the individual checkboxes?

I have a GridView with each row containing a checkbox, and the header of that column is a checkbox with checkAll functionality:

<Columns>
     <asp:TemplateField>
            <HeaderTemplate>
                   <input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);"                                       runat="server" type="checkbox" />
            </HeaderTemplate>
            <ItemTemplate>
                   <asp:CheckBox ID="chkSelect" runat="server" ToolTip="Select this order" AutoPostBack="true" OnCheckedChanged="chkSelect_OnCheckedChanged"/>
            </ItemTemplate>
     </asp:TemplateField>
     ...More BoundFields
</Columns>

The javascript behind the SelectAllCheckboxes()

function SelectAllCheckboxes(spanChk) {

        // Added as ASPX uses SPAN for checkbox

        var oItem = spanChk.children;
        var theBox = (spanChk.type == "checkbox") ?
    spanChk : spanChk.children.item[0];
        xState = theBox.checked;
        elm = theBox.form.elements;

        for (i = 0; i < elm.length; i++)
            if (elm[i].type == "checkbox" &&
          elm[i].id != theBox.id) {
                //elm[i].click();

                if (elm[i].checked != xState)
                    elm[i].click();
                //elm[i].checked=xState;

            }
    }

My GridView basically contains sales (orders) from my website, so dollar amounts. The OnCheckedChanged event adds (if checked) or subtracts (if unchecked) the current row's price from a totalAmount that is displayed on the page. This all works great except that when I click the checkAll checkbox, all of the OnCheckedChanged events for all of the row check boxes fire and it takes a long time to process it all. Since all that I am doing in the OnCheckedChanged method is summing amounts, is there a way that I can NOT call the events for the individual checkboxes and just make one call to a separate method that will grab all the GridView rows and sum them all at once?

If you consider doing it all on the server in one postback, you could try this:

<Columns>
    <asp:TemplateField>
         <HeaderTemplate>
              <asp:CheckBox ID="chkAll" runat="server" ToolTip="Select this order" AutoPostBack="true" OnCheckedChanged="chkAll_OnCheckedChanged"/>
         </HeaderTemplate>
         <ItemTemplate>
              <asp:CheckBox ID="chkSelect" runat="server" ToolTip="Select this order" AutoPostBack="true" OnCheckedChanged="chkSelect_OnCheckedChanged"/>
         </ItemTemplate>
    </asp:TemplateField>
</Columns>

On the code behind:

    protected void chkSelect_OnCheckedChanged(object sender, EventArgs e)
    { 
        //Process checked item 
    }

    protected void chkAll_OnCheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow item in grdTest.Rows)
        {
            CheckBox ckb = (CheckBox)item.FindControl("chkSelect");

            //This will not call the individual event
            ckb.Checked = ((CheckBox)sender).Checked;
            //Process checked item 
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM