Otavio de Souza Rocha
Otavio de Souza Rocha

Reputation: 203

Checkbox doesn't work on first click when checked by default

I have some checkboxes that doesn't work in first click when they are already checked by default.

What's happen is: When I click in te checked checkbox he fire Page_Load but doesn't fire OnCheckedChanged event and keep checked until I click again(after this all checkboxes start to work normally).

In the tests I made, I notice that if I keep the Panel witch include the checkboxes always visible this problem does not occur and if I remove the OnCheckedChanged event too

 <asp:UpdatePanel runat="server" ID="updApply">
    <ContentTemplate>
        <div class="row">
            <div class="col-md-12">
                <asp:Panel runat="server" ID="pnlApply">
                    <div class="row">
                        <div class="col-md-12">
                            <asp:Panel runat="server" ID="pnlAplicaDescontoEstabelecimento">
                                <div class="form-group">
                                    <div class="input-group">
                                        <div class="input-group-addon">
                                            <asp:CheckBox ID="cbxApply" runat="server" 
                                                onchange="loadUI();" 
                                                OnCheckedChanged="apply_CheckedChanged" 
                                                AutoPostBack="True" />
                                        </div>
                                    </div>
                                </div>                                                    
                            </asp:Panel>
                        </div>
                    </div>
                </asp:Panel>
            </div>
        </div>
    </ContentTemplate>
</asp:UpdatePanel>

I think it's important to say that in the code behind there are some functions that control whether the Panel is visible

Thanks!

Upvotes: 0

Views: 383

Answers (1)

ClearlyClueless
ClearlyClueless

Reputation: 762

Instead of using visible, try setting the css style display:none. Visible removes the control fronlm the pages markup entirely during render and may remove it's persisted value from viewstate resulting in the event not recognizing the changed value.

Another possible solution would be explicitly setting the default checked value to guarantee it makes it into viewstate, though it checked should default to false per the MSD.

Upvotes: 1

Related Questions