Click here to Skip to main content
15,887,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 inserting breakpoints in the code behind.

If I remove OnClick="return ProceedYN();" then the server side code is executed correctly.

Any suggestions on what I'm doing wrong are much appreciated!

What I have tried:

The relevant part of my code is:
ASP.NET
<ItemTemplate>
    <asp:CheckBox ID="InclSectionCB" OnClick="return ProceedYN();" OnCheckedChanged="InclSectionCBChanged" runat="server" AutoPostBack="True" />
</ItemTemplate>

JavaScript
<script type="text/javascript">
        function ProceedYN() {
            return confirm('Proceed?');
        }
    </script>
Posted
Updated 1-Nov-23 10:21am
v3
Comments
adriancs 31-Oct-23 7:53am    
I suggest use dynamic HTML and javascript ajax/fetchapi in stead.

If you view the rendered HTML in your browser, you will see that you have generated an onclick attribute that looks something like:
HTML
<input type="checkbox" onclick="return ProceedYN();WebForm_DoPostBackWithOptions(...);">
Clearly, regardless of the value returned from your ProceedYN function, the WebForm_DoPostBackWithOptions code will never be called, because your handler returns unconditionally.

Change your control to use:
ASPX
<asp:CheckBox ID="InclSectionCB" OnClick="if(!ProceedYN())return false;" OnCheckedChanged="InclSectionCBChanged" runat="server" AutoPostBack="True" />
Your generated HTML will then look more like:
HTML
<input type="checkbox" onclick="if(!ProceedYN())return false;WebForm_DoPostBackWithOptions(...);">
which will allow the WebForm_DoPostBackWithOptions to execute if your ProceedYN function returns true.
 
Share this answer
 
Comments
EStaples 31-Oct-23 18:08pm    
Thanks Richard, that's very concise & works perfectly.
The issue you're experiencing may be related to the order of execution and how the OnClick JavaScript event handler is interacting with the server-side event handler.
To fix this, You can remove the 'OnClick' javaScript event handler from the CheckBox control and use the OnClientClick attribute instead to bind the JavaScript function for confirmation.
ASP
<asp:CheckBox ID="InclSectionCB" OnCheckedChanged="InclSectionCBChanged" runat="server" AutoPostBack="True" OnClientClick="return ProceedYN();" />

In your JavaScript ProceedYN() function, you can show a confirmation dialog to the operator. If they confirm, you can return true, allowing the server-side event to execute. If they cancel, return false to prevent the postback.
JavaScript
function ProceedYN() {
    // Show a confirmation dialog to the operator
    var userConfirmed = confirm("Are you sure you want to proceed?");
    
    // Return true to allow the postback if confirmed, or false to cancel it
    return userConfirmed;
}
 
Share this answer
 
Comments
EStaples 31-Oct-23 18:08pm    
Thanks Imran, appreciate the response.
M Imran Ansari 1-Nov-23 2:39am    
You're welcome. Happy Coding!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900