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>
Related articles
Related discussion
-
Buy cheap Xanax overnight. Cheap Xanax. Overnight delivery of Xanax in US no prescription needed. Cheapest Xanax.
by asleymar (0 replies)
-
Buy Soma online without a prescription. Soma drug no prescription. How to get Soma prescription. Soma cod accepted.
by asleymar (0 replies)
-
Cheap online order Fioricet. Cheap discount Fioricet. Offshore Fioricet online. How to buy Fioricet online without a prescription.
by asleymar (0 replies)
-
Buy Ambien no visa without prescription. Not expensive Ambien prescriptions. Ambien no rx. Cod delivery Ambien.
by asleymar (0 replies)
-
Tramadol without doctor rx. Buy Tramadol over the counter cod overnight. Cheap Tramadol cod delivery. Buy Tramadol from mexico online.
by asleymar (0 replies)
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.
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
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.ClickPage.RegisterStartupScript("poscript", st)
End SubYour code will execute the onclick event twice!
You need to remove the onclick event specified in html.
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
"btnSubmit_Click" (The method in the code behind) needs to have public permissions
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;
}
Exactly what I needed to get started. Thanks, Ed.
It's very useful and pratical,
but a little short and simple
This thread is for discussions of Add a JavaScript popup to an ASP.NET button.