?
Solved

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

Posted on 2008-10-21
2
Medium Priority
?
3,797 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
[X]
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
2 Comments
 
LVL 16

Accepted Solution

by:
ororiole earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

770 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