Javascript Confirm using Server Tags

G

Guest

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
webform?

Below is an example:
====.aspx=======
<%@ Page language="c#" Codebehind="jsConfirmPopup.aspx.cs"
AutoEventWireup="false" Inherits="playground.jsConfirmPopup" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>jsConfirmPopup</title>
<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"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="JavaScript">
<!--
function okCancelConfirm(msg)
{
var answer = confirm (msg)
if (answer)
{
alert("T");
return true; // submit page
}
else
{
alert("F");
return false; // cancel page submission
}
}
//-->
</script>
</head>
<body MS_POSITIONING="GridLayout">
<form id="jsConfirmPopup" method="post" runat="server">
<asp:checkbox ID="chkBox1" AutoPostBack="True"
Runat="server"></asp:checkbox>
<input type="checkbox" id="chkBox2" onclick="return okCancelConfirm('Are
you sure you want to do this?')"></input>
</form>
</body>
</html>

===.cs===========
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.
//
InitializeComponent();
base.OnInit(e);
}

/// <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
System.EventHandler(this.chkBox1_CheckedChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top