469,903 Members | 1,461 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

Javascript Confirm using Server Tags

My goal is to have an ASP.NET checkbox server tag with it's autopostback set
to true, that when clicked will run the checkbox's CheckedChanged event.
However, I would like to try to add a javascript confirm popup to this
checkbox server control to prevent this event from firing if the cancel
button is checked in the js confirm window. In the example below, I am able
to accomplish this using the input type=checkbox but without the autopostback
feature; but, when using the asp:checkbox the cancel button does not prevent
the CheckedChanged event from firing but it does autopostback. I understand
the problem that the onclick attribute is being added in a span tag and the
checkbox's onclick is handling the postback, but not sure how to design
around this and accomplish the desired result....btw, is there a way to turn
off the compiler from autogenerating all the javascript postback code in a

Below is an example:
<%@ Page language="c#" Codebehind="jsConfirmPopup.aspx.cs"
AutoEventWireup="false" Inherits="playground.jsConfirmPopup" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
<script language="JavaScript">
function okCancelConfirm(msg)
var answer = confirm (msg)
if (answer)
return true; // submit page
return false; // cancel page submission
<body MS_POSITIONING="GridLayout">
<form id="jsConfirmPopup" method="post" runat="server">
<asp:checkbox ID="chkBox1" AutoPostBack="True"
<input type="checkbox" id="chkBox2" onclick="return okCancelConfirm('Are
you sure you want to do this?')"></input>

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace playground
/// <summary>
/// Summary description for jsConfirmPopup.
/// </summary>
public class jsConfirmPopup : System.Web.UI.Page
protected System.Web.UI.WebControls.CheckBox chkBox1;

private void Page_Load(object sender, System.EventArgs e)
chkBox1.Attributes.Add("onClick","return okCancelConfirm('Are you sure
you want to do this?')");


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
// CODEGEN: This call is required by the ASP.NET Web Form Designer.

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
this.chkBox1.CheckedChanged += new
this.Load += new System.EventHandler(this.Page_Load);


private void chkBox1_CheckedChanged(object sender, System.EventArgs e)
Response.Write("Checked Changed Event Fired");

Nov 18 '05 #1
0 3556

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.