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
-
Getting Culture Information through Javascript
by kishorrudra (5 replies)
-
How to assign a value to hidden field using javascript in asp.net
by Freon22 (8 replies)
-
Add a JavaScript popup to an ASP.NET button
by jessics (9 replies)
-
How to access a javascript variable from Csharp code?
by laltu4pdey (1 replies)
-
Java Script, File uploading on ftp server using java script code
by James Crowley (1 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
-
Nov
20
Full Frontal JavaScript Conference
Brighton, United Kingdom
A one day JavaScript conference held in Brighton, UK whose essence is to discuss JavaScript "with nothing concealed or held back".The conference is being held at one of the world's first cinemas, which first opened in 1910.Speakers include...
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.