Click here to Skip to main content
15,906,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I want to be able to display a confirmation message when asp.net checkbox is clicked, i tried using:
C#
OnClientClick = "return confirm('Are you sure you want to mark register?')"
aspx code:
C#
<asp:CheckBox ID="chkSignIn" runat="server" Text="Sign Out" AutoPostBack="true" OnCheckedChanged="chkSignIn_CheckedChanged" OnClientClick= "return confirm('Are you sure you want to mark register?')" />
it doesnt report error, but not working, i later found out that asp.net checkbox doen't have OnClientClick attribute, i have also done a few research on google still couldn't achieve! Please does anyone know how to do this? Thanks in advance.
Posted
Updated 13-Apr-15 5:08am
v3

Combining onclick with AutoPostBack can be tricky. By adding an unconditional return statement, you end up bypassing the code which causes the page to post back to the server.

Try this:
ASP.NET
<asp:CheckBox ID="chkSignIn" runat="server" Text="Sign Out" AutoPostBack="true" OnCheckedChanged="chkSignIn_CheckedChanged" OnClick="if(!confirm('Are you sure you want to mark register?'))return false;" />
 
Share this answer
 
Use onclick instead.

JavaScript
onclick= "return confirm('Are you sure you want to logout?')"
 
Share this answer
 
Comments
Uwakpeter 13-Apr-15 10:52am    
OnClick is not working too! it brings out the comfirmation box, but if i clicked ok, the checkbox event is not triggerd!
ZurdoDev 13-Apr-15 10:55am    
Post your .aspx code of your checkbox so we can see what you have then.
Uwakpeter 13-Apr-15 11:06am    
I have updated the question, with onlick method, the confirmation displays when the box is unchecked, i want it to display on when the box is checked!
ZurdoDev 13-Apr-15 11:13am    
Your updated question is still using OnClientClick which does not work on a checkbox.

Using onclick will fire whenever it is clicked. It will fire when checked or unchecked. If you want to have logic that checks what the value is, you can do that. I suggest googling for checking the value of checkbox in jQuery if you are not familiar with how.
Uwakpeter 13-Apr-15 11:34am    
onclick event doesnt fire when ok is clicked

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