Solved

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

Posted on 2008-10-21
2
3,640 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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