Solved

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

Posted on 2008-10-21
2
3,592 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
Comment Utility
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
Comment Utility
Thanks so much, that worked perfectly!!!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now