vendredi 25 septembre 2015

How do I retain disabled checkboxes upon postback?

I have this set of javascript codes which allows me to disable/enable checkboxes according to my selection.

The 1st set of codes below allows me to set the default as "Select All" and disables the rest when "Select All" is checked during the 1st page load but not on postback. The reason to not do it in post back is because e.g If a user did not check any checkbox in checkboxlist2, I want to prompt the user the select the check at least one checkbox in checkboxlist 2 but do not want to overwrite the user selection previously on postback with the default "Select All" in checkboxlist1 and checkboxlist2.

$(function () {
    if($("#hidden").val() == "")
    {
        $("#Checkboxlist1 :checkbox").attr('disabled', 'disabled');
        $("#Checkboxlist1 :checkbox[value='Select All']").removeAttr('disabled');
        $("#Checkboxlist1 :checkbox[value='Select All']").prop("checked", true);
        $("#Checkboxlist2 :checkbox").attr('disabled', 'disabled');
        $("#Checkboxlist2 :checkbox[value='Select All']").removeAttr('disabled');
        $("#Checkboxlist2 :checkbox[value='Select All']").prop("checked", true);
        $("#hidden").val("set");
    }
});

$(function () {
    $("#Checkboxlist2 :checkbox").change(function () {
            var ischecked = $(this).is(":checked");
            var val = $(this).val();
            //alert(val);
            if (val == "Select All") {
                if (ischecked) {
                    $("#Checkboxlist2 :checkbox").attr('disabled', 'disabled');
                    $(this).removeAttr('disabled');
                    $("#Checkboxlist2  :checkbox").prop("checked", false);
                    $(this).prop("checked", true);

                    return;
                } else {
                    $("#Checkboxlist2  :checkbox").removeAttr('disabled');
                    return;

My question is:

upon postback my check values retains but the checkboxes disabled previously are enabled so how do I retain its whole state upon postback?

I did enabled my checkboxlist enableviewstate = true




Aucun commentaire:

Enregistrer un commentaire