2

I just stuck on manipulating with checkboxes, i have one Gridview having first column as checkbox in header as well as in rows, Checkboxes are bound with iID value from the database. I want to delete rows sometimes "All" (by Header checkbox) and sometimes "Selected" (by checkbox). right now i am using this code:

JavaScript

function checkcheckboxPrint() {
            var checkfag = false;
            var id = 0;
            var table = document.getElementById('<%=grdRefPhysician.ClientID%>');

            if (table != null) {
                for (i = 1; i < table.rows.length; i++) {
                    var chkRead = table.rows[i].cells[0].getElementsByTagName("input")[0];
                    var DivID = table.rows[i].cells[0].getElementsByTagName('div')[0];

                    if (chkRead != null) {
                        if (chkRead.type == 'checkbox') {
                            if (chkRead.checked) {
                                id = DivID.innerHTML + "," + id;
                                checkfag = true;
                                alert(id);
                            }
                        }
                    }
                }
                if (checkfag == true) {
                    $("#<%=hdnIDs.ClientID %>").val(id);
                    return true;
                }
                else {
                    alert("Please select record to delete.");
                    return false;
                }
            }
        }

jQuery

    $(document).ready(function () {
        $("input:checkbox").change(function () {
            $("#DivDelete").show('slow');
        });
    });

As you can see i am using Hidden field to get those comma (,) separated IDs to server side, basically its working but, i am not going through one scenario that if i check header row then all checkboxes should be selected, if i uncheck it then all checkboxes should be deselected like viceversa, i am calling this js method on click of this

HTML

 <div id="DivDelete" style="text-align:left;display:none;">
        <asp:Button ID="btnDeleteAll" OnClientClick="return checkcheckboxPrint();" runat="server" Text="Delete" />
 </div>

i just wanted to store comma separated IDs to hidden field using jQuery, with checkAll and independent check facility!

Attached Screen shot of my Grid

enter image description here

Any help is appreciated! Thanks

Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68

1 Answers1

0

I dont't think

table.rows[i].cells[0]

is going to work on a js object. What you have to do is add a change handler to every checkbox, update an array with the selected id's to be deleted (you can find the id by using .parent().find(yourIdDiv) on the current checkbox. Check the length of the array when user clicks DeleteAll and proceed acordingly.

sdagkas
  • 578
  • 1
  • 5
  • 20