Library code snippets

Add a JavaScript popup to an ASP.NET button

If you want an easy way to remind your user to check if all the information on a form is correct after clicking the button, or if you want a second "are you sure?" question to appear after clicking a button before a significant event is carried out (deleting a large amount of records), then you can use this code to add a JavaScript alert pop-up to any ASP.NET button control. Works in IE, don't know about the others yet.

<%@ Page Language="C#" %>
<script runat="server">
public void Page_Load(Object sender, EventArgs E) {
btnSubmit.Attributes.Add("onclick","javascript:if(confirm('Are you sure everything is correct?')== false) return false;");
}
void btnSubmit_Click(object sender, EventArgs e) {
        Message.Text = "You entered your name as: " + txtName.Text;
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
    Name: <asp:Textbox id="txtName" runat="server"/>
<asp:Button id="btnSubmit" onclick="btnSubmit_Click" runat="server" Text="Submit"></asp:Button><br/>
<asp:Label id="Message" runat="server"/>
</form>
</body>
</html>

Comments

  1. 18 Mar 2009 at 12:29
    Hi,I’m just wondering, how do you go around the EvaluateEvent error? It appears that by default on click events are evaulated by the .NET Runtime, and this traps my On Client Click event. If I set it to false craps gambling, some of the events on my page won’t be processed properly. How do you go about that?
  2. 27 May 2008 at 13:22

    IE7 (Vista) has problems with the java confirm script in this form. After a lot of testing and research we came to this sollution that works for IE7 (Vista), IE (other), Firefox

     

    if(confirm(\"Are You Sure\")==false){ try{window.event.returnValue = false;}catch(err){}return false; };"); 

     

    the "try catch" is because -- "window.event.returnValue = false;" -- gives problems in firefox (and firefox based browsers)

    Hope this help everyone with the same problem (and wants to update for the new Microsoft(c) Vista(c) Errors(c) )

     

    Michiel 

  3. 04 Dec 2006 at 03:12

    Another way to get popup is to write the scripts right inside the header as below.

    <HEAD>
    <script type="text/javascript" language="JavaScript">
    function openph(url)
    {
    var url1 = url;
    window.open(url1, "","top=0,left=0,menubar=no,toolbar=no,location=no,resizable=no,height=550,width=440,status=no,scrollbars=no,maximize=null,resizable=0,titlebar=no;");
    }
    </script>






     

    Call any function from the code behind using the following

    Dim st As String = "<script language='javascript'>" & _

    "openph('Enlargephoto.aspx')" & _

    "</script>"

    Call this from a button click like this.

    Private

    Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

    Page.RegisterStartupScript("poscript", st)

    End Sub

  4. 13 Jul 2006 at 00:11

    Your code will execute the onclick event twice!

    You need to remove the onclick event specified in html.

     

  5. 13 Aug 2005 at 01:43

    HI,
    this is good code
    I like it.
    but you dint mention that what name space need to use for stringbuild ????


    any way I used this   using System.Te

  6. 28 Apr 2005 at 16:35

    "btnSubmit_Click" (The method in the code behind) needs to have public permissions

  7. 28 Feb 2005 at 22:26

    sometimes it is neccessary to add return value otherwise the code behind event will be executed.


    if (confirm("Are you sure ?") == false)
    {
     window.event.returnValue = false;
     return false;
    }
    else
    {
     window.event.returnValue = true;
     return true;
    }

  8. 24 May 2004 at 07:04

    Exactly what I needed to get started.  Thanks, Ed.

  9. 14 Mar 2004 at 02:28

    It's very useful and pratical,
    but a little short and simple

  10. 01 Jan 1999 at 00:00

    This thread is for discussions of Add a JavaScript popup to an ASP.NET button.

Leave a comment

Sign in or Join us (it's free).

Edward Tanguay Edward Tanguay updates his personal web site tanguay.info weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.

Related podcasts

  • Kito Mann Interview

    Ted Neward talks with Kito (JSFCentral) Mann about, yes you guessed it, Java Server Faces. What is the current state of JSF, what's the impact of Javascript and Ruby on the JEE5 presentation tier and how does it compare to ASP.NET are just a handful of questions that are fired by Ted. JavaSer...

Events coming up

  • May 19

    Google I/O 2010

    San Francisco, United States

    Google's largest developer event returns to San Francisco in 2010. Google I/O brings together thousands of developers for two days of highly technical content, focused on pushing the boundaries of web applications through open web technologies and Google developer products like App Engine, Google Web Toolkit, Android, Chrome, APIs, and more. Early registration for Google I/O will open in January 2010.

We'd love to hear what you think! Submit ideas or give us feedback