Javascript Confirm using Server Tags

Discussion in 'ASP .Net' started by =?Utf-8?B?Q2hyaXMgRmluaw==?=, Nov 16, 2004.

  1. 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");
    }
    }
    }
     
    =?Utf-8?B?Q2hyaXMgRmluaw==?=, Nov 16, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dean H. Saxe
    Replies:
    0
    Views:
    1,055
    Dean H. Saxe
    Jan 3, 2004
  2. Kathy Burke
    Replies:
    2
    Views:
    14,305
    Kathy Burke
    Dec 29, 2003
  3. =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=

    Run server code on a javascript confirm

    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=, Dec 8, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    5,406
    Matt Berther
    Dec 9, 2004
  4. Rob Nicholson
    Replies:
    3
    Views:
    780
    Rob Nicholson
    May 28, 2005
  5. Replies:
    1
    Views:
    123
    Peter Michaux
    Mar 20, 2008
Loading...

Share This Page