RockiesMagicNumber
RockiesMagicNumber

Reputation: 501

Javascript not recognizing Asp Checkbox Checked Property

I have a list of individual checkboxes in an ASP form:

<asp:UpdatePanel runat="server" ID="FiltersUpdPnl">
    <ContentTemplate>
        <div class="filters">
            Show: 
            <asp:DropDownList runat="server" ID="CapNumProjectsDDL" AutoPostBack="true" OnSelectedIndexChanged="GenericFiltersChanged" >
                <%--<asp:ListItem Value="0" Text="" Selected="True"></asp:ListItem>--%>
                <asp:ListItem Value="1" Text="Capacity"></asp:ListItem>
                <asp:ListItem Value="2" Text="Number of Projects"></asp:ListItem>
            </asp:DropDownList>
        </div>
        <div id="filterlist" class="filters">
            <span style="font-size:13pt;display:none;">Filters:<asp:Button runat="server" 
                ID="ApplyFilters1Btn" Text="Apply New Filters" Visible="false" OnClick="ApplyFilters" /> </span>
            <br />
            <span style="font-size:10pt;">Project Type:</span>
            <ul>
                <li>
                    <asp:CheckBox AutoPostBack="true" runat="server" ID="ShowAllChkBx" Text="(check/uncheck all)" 
                        Checked="false" oncheckedchanged="ShowAllChkBx_CheckedChanged" />
                </li>
            </ul>
            <div id="filterchks" runat="server">
                <ul>
                    <li><asp:CheckBox AutoPostBack="true" runat="server" ID="BiomassChkBx" Text="Biomass" Checked="true" Value="7" OnClick="JavaScript:ParentCheck(parent, 'Biomasschks')"/></li>
                        <div runat="server" id="Biomasschks">
                            <ul>
                                <li><asp:CheckBox AutoPostBack="true" runat="server" ID="BiomassUnspChkBx" Text="Biomass" Checked="true" Value="18" OnClick="JavaScript:uncheckParent(this,'BiomassChkBx')"/></li>
                                <li><asp:CheckBox AutoPostBack="true" runat="server" ID="BiomassLandfillGasChkBx" Text="Biomass - Landfill Gas" Checked="true" Value="29" OnClick="JavaScript:uncheckParent(this,'BiomassChkBx')"/></li>
                            </ul>
                        </div>
                    <li><asp:CheckBox AutoPostBack="true" runat="server" ID="CSPChkBx" Text="CSP" Checked="true" Value="11" OnClick="JavaScript:ParentCheck(parent, 'CSPchks')" /></li>
                        <ul>
                            <div runat="server" id="CSPchks">
                                <li><asp:CheckBox AutoPostBack="true" runat="server" ID="CheckBox2" Text="CSP" Checked="true" Value="5" OnClick="JavaScript:uncheckParent(this,'CSPChkBx')" /></li>
                                <li><asp:CheckBox AutoPostBack="true" runat="server" ID="CSPTroughChkBx" Text="CSP - Trough" Checked="true" Value="35" OnClick="JavaScript:uncheckParent(this,'CSPChkBx')" /></li>
                                <li><asp:CheckBox AutoPostBack="true" runat="server" ID="CSPLensChkBx" Text="CSP - Lens" Checked="true" Value="36" OnClick="JavaScript:uncheckParent(this,'CSPChkBx')" /></li>
                                <li><asp:CheckBox AutoPostBack="true" runat="server" ID="CSPTowerChkBx" Text="CSP - Tower" Checked="true" Value="37" OnClick="JavaScript:uncheckParent(this,'CSPChkBx')" /></li>
                                <li><asp:CheckBox AutoPostBack="true" runat="server" ID="CSPDishEngineChkBx" Text="CSP - Dish Engine" Checked="true" Value="38" OnClick="JavaScript:uncheckParent(this,'CSPChkBx')" /></li>
                                <li><asp:CheckBox AutoPostBack="true" runat="server" ID="CSPLinearFresnelChkBx" Text="CSP - Linear Fresnel" Checked="true" Value="39" OnClick="JavaScript:uncheckParent(this,'CSPChkBx')" /></li>
                            </div>
                        </ul>
                    <li><asp:CheckBox AutoPostBack="true" runat="server" ID="GeothermalChkBx" Text="Geothermal" Checked="true" Value="8"/></li>
                    <li><asp:CheckBox AutoPostBack="true" runat="server" ID="HydroChkBx" Text="Hydro" Checked="true" Value="13"/></li>
                    <li><asp:CheckBox AutoPostBack="true" runat="server" ID="OceanChkBx" Text="Ocean" Checked="true" Value="9" OnClick="JavaScript:ParentCheck(parent, 'OceanChks')"/></li>
                        <ul>
                            <div runat="server" id="OceanChks">
                                <li><asp:CheckBox AutoPostBack="true" runat="server" ID="CheckBox1" Text="Ocean" Checked="true" Value="3" OnClick="JavaScript:uncheckParent(this,'OceanChkBx')"/></li>
                                <li><asp:CheckBox AutoPostBack="true" runat="server" ID="OceanWaveChkBx" Text="Ocean - Wave" Checked="true" Value="45" OnClick="JavaScript:uncheckParent(this,'OceanChkBx')" /></li>
                                <li><asp:CheckBox AutoPostBack="true" runat="server" ID="OceanTidalCurrentChkBx" Text="Ocean - Tidal/Current" Checked="true" Value="46" OnClick="JavaScript:uncheckParent(this,'OceanChkBx')" /></li>
                            </div>
                        </ul>
                    <li><asp:CheckBox AutoPostBack="true" runat="server" ID="SolarPVChkBx" Text="Solar PV" Checked="true" Value="10"/></li>
                    <li><asp:CheckBox AutoPostBack="true" runat="server" ID="WindChkBx" Text="Wind" Checked="true" Value="12" OnClick="JavaScript:ParentCheck(parent, 'WindChks')"/></li>   
                        <ul>
                            <div runat="server" id="WindChks">
                                <li><asp:CheckBox AutoPostBack="true" runat="server" ID="WindOnshoreChkBx" Text="Wind - Onshore" Checked="true" Value="6" OnClick="JavaScript:uncheckParent(this,'WindChkBx')" /></li>
                                <li><asp:CheckBox AutoPostBack="true" runat="server" ID="WindOffshoreChkBx" Text="Wind - Offshore" Checked="true" Value="21" OnClick="JavaScript:uncheckParent(this,'WindChkBx')" /></li>
                            </div>
                        </ul>                             
                </ul>
            </div>
        </div>
        <div runat="server" id="StatusDiv" class="filters">
                <span style="font-size:10pt;">Status:</span>
                <ul>
                    <li><asp:CheckBox AutoPostBack="true" runat="server" Value="1" ID="OperatingChkBx" Text="Operating" Checked="true" OnCheckedChanged="GenericFiltersChanged" /></li>
                    <li><asp:CheckBox AutoPostBack="true" runat="server" Value="2" ID="UnderConstructionChkBx" Text="Under Construction" Checked="true" OnCheckedChanged="GenericFiltersChanged" /></li>
                    <li><asp:CheckBox AutoPostBack="true" runat="server" Value="3" ID="PreConstructionChkBx" Text="Pre-Construction" Checked="true" OnCheckedChanged="GenericFiltersChanged" /></li>
                </ul>
            <br />
        </div>
    </ContentTemplate>
</asp:UpdatePanel>

The "UncheckParent" functionality is working properly, but when the parent is checked/unchecked, it's supposed to change all of the checkboxes inside its corresponding div to its checked status (that is, parent checked, children checked, and vice versa):

function uncheckParent(checkbox, parentcheckboxid) {
    var Parentcheckbox = document.getElementById(parentcheckboxid);

    if (!checkbox.checked) {
        Parentcheckbox.checked = false;
    }
}

function ParentCheck(parent, aId) {
    if (parent.checked == false) {
        checkByParent(aId, false);
        alert("false");
    }
    else if (parent.checked == true) {
        checkByParent(aId, true);
    }

}

function checkByParent(aId, aChecked) {
    var collection = document.getElementById(aId).getElementsByTagName('INPUT');
    for (var x = 0; x < collection.length; x++) {
        if (collection[x].type.toUpperCase() == 'CHECKBOX')
            collection[x].checked = aChecked;
    }

}

As coded, the function always returns that the parent checkbox' checked property == false, as evidenced by that "false" alert (which is only there for the sake of debugging).

Why is this returning false no matter what status the parent checkbox's checked property? I tried removing the "Checked="true"" Property from all of the checkboxes to see if that was what the problem was, but alas, the functionality remained the same.

(I had this coded server-side at first, which worked great, but I had to rearrange the page a bit and change postback events, and the server was reading the checked property on Page_Load before the Event Handler had a chance to fire.)

Upvotes: 1

Views: 5009

Answers (1)

Aristos
Aristos

Reputation: 66639

I will focus on one line, and the rest are similar.
So in this line you have some main issues.

<asp:CheckBox AutoPostBack="true" runat="server" ID="CSPChkBx" Text="CSP" 
 Checked="true" Value="11" OnClick="JavaScript:ParentCheck(parent, 'CSPchks')" />
  1. You send the wrong id. To make the id works, you must place the ClientID as static on asp.net 4, or send the this keyword on the parent() function
  2. Your javascript will not work because you make post back and what ever you do, is gone. So remove ether the autopostback, ether remove the javascript.
  3. Also the asp.net check control is render an input together with a span. So the onclick is may reference to the span, and not on the input control, you need to see the source code to see where is go.

So you need to re-design what you like to do here.

Upvotes: 2

Related Questions