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>
AddThis

Comments

  1. 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 

  2. 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

  3. 13 Jul 2006 at 00:11

    Your code will execute the onclick event twice!

    You need to remove the onclick event specified in html.

     

  4. 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

  5. 28 Apr 2005 at 16:35

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

  6. 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;
    }

  7. 24 May 2004 at 07:04

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

  8. 14 Mar 2004 at 02:28

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

Leave a comment

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

Events coming up

  • Oct 14

    What’s New in Visual Studio 2008 Service Pack 1?

    Birmingham, United Kingdom

    “Service Pack? We’re calling it a Service Pack? Are you kidding??!?!” Visual Studio 2008 Service Pack 1 will release later in 2008 alongside .NET Framework V3.5 Service Pack 1 and, together, they represent a significant upgrade to Visual Studio 2008. There are enhancements across many areas of the .NET Framework such as data access, windows application development and web development and there are also corresponding changes in the development environment to support the new framework features.