Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2008-10-21
2
Medium Priority
?
3,939 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

581 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