Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to add a javascript confirm to a checkbox with a checkedChange event

Posted on 2008-10-21
2
3,664 Views
Last Modified: 2010-04-21
I have a checkbox that I need to add a client side confirm before firing the checkedChanged event. THe problem is that the checked change event is not firing after the javascript.  Currently I don't have the CheckedChanged event doing anything, I just have a breakpoint making sure it gets there.

Thanks!

Here is my code:

On my webpage:
 
<asp:CheckBox ID="chkAllData" Text="All Information Has Been Entered" 
runat="server" AutoPostBack="True" CausesValidation="False" />
 
In the page load:
 Me.chkAllData.Attributes.Add("onclick", "return confirm('Are you sure you want to delete?');")
 
Then the CheckedChanged method:
Protected Sub chkAllData_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAllData.CheckedChanged
  If AccidentID.Value <> "" Then
  End If
End Sub

Open in new window

0
Comment
Question by:Kathryn77
2 Comments
 
LVL 16

Accepted Solution

by:
ororiole earned 500 total points
ID: 22773052
Whoa, THAT turned out to be way harder than I thought it would be. The <asp: controls checkbox and radiobutton use the onclick event as the postback event. And unlike other controls they do not implement an onclientclick event for client-side only processing. So once you assign onclick to a clientside function, it can no longer postback to the server. Therefore, you must do that yourself.

I have attached my example, you should have no trouble adapting it to your needs. :) Note that autopostback is now false (the default), we do not modify the attributes in page_load, it is all done in the markup. The Handles clause is omitted, it will give you problems. Let me know if you have any questions.
	Protected Sub Checkbox2_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
 
	End Sub
 
aspx file:
<script type="text/javascript">
// <!CDATA[
	function doConfirm(cbox) {
	
		var id = '<%=Checkbox2.ClientID %>';
	
		if (confirm('Yo?')) {
 
			__doPostBack(id, cbox.checked);
			return true;
		}
		else {
			return false;
		}
	}
// ]]>
</script>
		<asp:CheckBox ID="CheckBox2" runat="server" 
			onclick="javascript:doConfirm(this);" 
			OnCheckedChanged="Checkbox2_CheckedChanged" />

Open in new window

0
 

Author Closing Comment

by:Kathryn77
ID: 31508485
Thanks so much, that worked perfectly!!!
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question