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
-
How to pass value from a Pop up window to Content Page in asp.net C#pl
by Mulish Mehdi (5 replies)
-
javascript calender in datagrid
by ramdhavepreetam (1 replies)
-
How to call ASp.Net Vb Class function By JavaScript
by gitsiva (2 replies)
-
Debug javascript
by anilsjce (0 replies)
-
How to access a javascript variable from Csharp code?
by laltu4pdey (1 replies)
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.
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