0

i am using asp.net gridview in header i have two columns

column select all employee  | column select all employer

so i have two checkbox for two columns, so once it click on the select all employee it should only be selected for all employee and same goes with employer

but the below code is selecting both employee and employer if only select on employer.

function SelectAllCheckboxes(chk) {
        $('#<%=gv.ClientID%>').find("input:checkbox").each(function () {
            if (this != chk) { this.checked = chk.checked; }
        });
    }

    function SelectAllCheckboxes1(chk) {
        $('#<%=gv.ClientID%>').find("input:checkbox").each(function () {
            if (this != chk) { this.checked = chk.checked; }
        });
    }


<asp:CheckBox ID="chkAll" runat="server" onclick="javascript:SelectAllCheckboxes(this);" />

<asp:CheckBox ID="chkAll1" runat="server" onclick="javascript:SelectAllCheckboxes1(this);" />
5
  • What do you mean? When you click on Select All Employers, the Select All Employees gets checked too? Commented May 7, 2012 at 20:17
  • Well that makes sense, because you are not differentiating between the two checkboxes. Add a class to the employer checkbox of "employer" on the server side then, select "input:checkbox.employer" Commented May 7, 2012 at 20:18
  • i have two columns in the gridview header and if click on column 1 the column 1 checkbox should be select and if i click on column 2 then column 2 checkbox should be select, but in my case if i select on column 1 then column 1 and column 2 checkbox is selected Commented May 7, 2012 at 20:19
  • right, because they are all checkboxes. You aren't being specific enough. On each one you are saying the same thing, GridView->inputs that are checkboxes Commented May 7, 2012 at 20:20
  • yes i am aware of that i am not being specific what checkbox to check... thats why i posted my question for any lead Commented May 7, 2012 at 20:22

1 Answer 1

1

ASP - in your GridView:

<asp:GridView id="gv" runat="server">
    <asp:TemplateField>
        <ItemTemplate>
            <asp:CheckBox id="cbxSelectEmployer" runat="server" CssClass="employer" />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:CheckBox id="cbxSelectEmployee" runat="server" CssClass="employee" />
        </ItemTemplate>
    </asp:TemplateField>
</asp:GridView>

ASP - "Select all" checkboxes:

<asp:CheckBox ID="chkAll" runat="server" onclick="SelectAllCheckboxes(this, '.employee')" />
<asp:CheckBox ID="chkAll1" runat="server" onclick="SelectAllCheckboxes(this, '.employer')" />

jQuery

function SelectAllCheckboxes(chk, selector) {
    $('#<%=gv.ClientID%>').find(selector + " input:checkbox").each(function () {
        $(this).prop("checked", $(chk).prop("checked"));
    });
}
Sign up to request clarification or add additional context in comments.

5 Comments

okay so paste your code and when i click on the checkbox which is in the header does not do anything no errors nothing
then i pass just like that 'onclick="SelectAllCheckboxes(this, 'employee')"' (take out dot) then it throw an error here Microsoft JScript runtime error: Exception thrown and not caught
found the error. when you specify a cssclass it wraps it in a span with that class. I will edit
I switched the places, so it should work now. keep the dot, it means that jquery will select the classes
1+ yep its working now.. but i have another question i guess i will open a new thread - thanks for helping out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.