mardi 31 octobre 2023

When asp.net CheckBox control is changed, how to fire client side AND server side events

I have an asp.net Checkbox inside a gridview. It has some javascript in OnClick to confirm that the operator wishes to proceed. It also has some server side code on the OnCheckedChanged event.

The client side code is being executed, but the server side code is not being triggered, irrespective of whether the operator elects to proceed or not. I have confirmed this by adding some breakpoints.

If I remove the OnClick client side code, the server side code is executed correctly.

Any suggestions on what I'm doing wrong would be very much appreciated.

Code below:

 <asp:CheckBox ID="InclSectionCB" OnClick="return ProceedYN();" OnCheckedChanged="InclSectionCBChanged" runat="server" AutoPostBack="True" />
    <script type="text/javascript">
        function ProceedYN() {
            return confirm('Proceed?');
        }
    </script>

I expected that upon checking/unchecking the checkBox that the steps would be:

  1. Client side javascript prompts the operator if they would like to proceed. 2a) If operator selects 'Ok', then Checkbox would be changed and the server side code in InclSectionCBChanged would be executed. 2b) If operator selects 'Cancel', then Checkbox would not be changed and the server side code in InclSectionCBChanged would not be executed.

What is happening is:

  1. Client side javascript prompts the operator if they would like to proceed. (correct) 2a) If operator selects 'Ok', then Checkbox is changed but the server side code in InclSectionCBChanged is not executed. 2b) If operator selects 'Cancel', then Checkbox is not changed and the server side code in InclSectionCBChanged is not executed.



Aucun commentaire:

Enregistrer un commentaire