Make Sure Item is Selected For Deletion in ListView Prior to Post Back using jQuery

by Bill Beckelman 30. September 2008 22:15

I have been adding the ability to delete multiple items in a ListView for a while now. One thing that has been bugging me though was the user could click the button to delete the selected items even if no items were selected which would popup a delete confirmation. If the user clicked yes, then a post back would occur and be confusing.

Demo | Download

End Product:

image 

jQuery:

    $(document).ready(function() {
        checkButtonStatus();

        $(".selectAll >input:checkbox").click(function() {
            checkButtonStatus();
        });

    });

    function checkButtonStatus() {
        var count = 0;
        $(".selectAll >input:checkbox").each(function() {
            if (this.checked == true) {
                count++;
            }
        });

        if (count != 0) {
            $("#ButtonDeleteSelected").removeAttr("disabled");           
        }
        else {
            $("#ButtonDeleteSelected").attr("disabled", "disabled");            
        }    
    } 

Whenever a checkbox is checked or un-checked the function checkButtonStatus() is run. The function is also run on document ready. The function takes care of checking whether the button should be enabled or not.

Be sure to check out the Demo  and the Download

kick it on DotNetKicks.com     

 

Tags:

ASP.NET | jQuery

Comments


About Me

I live and work in Salt Lake City, Utah. My background is in aviation. I have a degree in Aeronautical Science from Embry-Riddle Aeronautical University in Prescott, AZ. I have worked as a commercial airline pilot and most recently as a technical advisor for a charter airline.