Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to pop a message box in ASP.NET page

1 view
Skip to first unread message

kai

unread,
Jan 6, 2004, 12:24:43 AM1/6/04
to
Hi, All
I am trying to pop a message box in ASP.NET using JavaScript when I click
a button, but I realize the JavaScript works for HTML control button do not
work for ASP button.

Any idea how to use JavaScript to trigger a message box for ASP.NET button?

Thanks for any help.

Kai

Steven Cheng[MSFT]

unread,
Jan 6, 2004, 1:53:33 AM1/6/04
to
Hi kai,


Thank you for using Microsoft Newsgroup Service. Based on your description,
you want to popup a message box when a
ASP.NET server button is clicked. Is my understanding of your problem
correct?

If so, you may try use the ServerControl's "Attributes" collection property
to add the client side event binding. For example:

You've a ASP.NET button in page named "btnPop"

then in the page class, add such code:

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

btnPop.Attributes.Add("onclick","alert(\"hello\")");
}

Then when the server button is clicked, it'll first run the client side
script and then post back to the server side. You may have a try to check
out the above suggestion.

If you have any questions on it, please feel free to let me know.


Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Steve C. Orr [MVP, MCSD]

unread,
Jan 6, 2004, 3:25:56 AM1/6/04
to
Execute a line of code like this when you want a message box to be
displayed.
(This writes out the necessary client side javascript to your HTML page to
make the alert pop up as soon as the page is sent to their browser.)

RegisterStartupScript("startupScript", "<script
language=JavaScript>alert('This is my message.');</script>");

Here's more info:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIPageClassRegisterStartupScriptTopic.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com

"kai" <kail...@earthlink.net> wrote in message
news:vErKb.11468$6B....@newsread1.news.atl.earthlink.net...

kai

unread,
Jan 6, 2004, 8:44:07 AM1/6/04
to
Steve,
Thanks for your help. I will give a try.

Kai
"Steve C. Orr [MVP, MCSD]" <St...@Orr.net> wrote in message
news:%23PtE26C...@TK2MSFTNGP09.phx.gbl...

kai

unread,
Jan 6, 2004, 8:44:46 AM1/6/04
to
Hi, Steven

Thanks for your help. I will give a try.
Kai
"Steven Cheng[MSFT]" <v-sc...@online.microsoft.com> wrote in message
news:lQQsSHC1...@cpmsftngxa07.phx.gbl...

kai

unread,
Jan 6, 2004, 9:46:18 AM1/6/04
to

Hi, Steven

I implement your suggestion using VB.NET code:

Public Class WebForm1
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load


btnPop.Attributes.Add("onclick", "alert(\'hello\')")

End Sub
End Class


HTML code is:


<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb" Inherits="WebApplication6.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Button id="btnPop" runat="server"></asp:Button>
</form>
</body>
</HTML>

I get error message:

"Run time error has occured. Do you whish to debug?
Line 10
Error: Invalid character"

What did I do wrong?

Thank you veru much for helping.


Kai


"Steven Cheng[MSFT]" <v-sc...@online.microsoft.com> wrote in message
news:lQQsSHC1...@cpmsftngxa07.phx.gbl...

Bob Lehmann

unread,
Jan 6, 2004, 10:26:07 AM1/6/04
to
Either remove the escape characters - \ - from the single ticks.

btnPop.Attributes.Add("onclick", "alert('hello')")

Or write it as in the example you were given with regular quotes.
btnPop.Attributes.Add("onclick","alert(\"hello\")");

Bob Lehmann

"kai" <kail...@earthlink.net> wrote in message

news:_SzKb.11826$6B....@newsread1.news.atl.earthlink.net...

kai

unread,
Jan 6, 2004, 12:15:06 PM1/6/04
to
Hi, Bob
It works!!! Thanks


Kai
"Bob Lehmann" <none> wrote in message
news:%23xQhQlG...@TK2MSFTNGP12.phx.gbl...

bassil

unread,
Mar 16, 2004, 12:56:07 AM3/16/04
to
You can try this one:

MessageBox for ASP.NET from http://www.bassilsoft.com

It have a cool pop up messagebox feature that you wanted.

You can find the source code in its samples when finish installing it.

Gönen EREN

unread,
Mar 16, 2004, 2:13:34 AM3/16/04
to
It depends on your needs...
Instead of using this component you can simply popup via javascipt a new
window with fixed size and no scroll. and foece the focus on it...


"bassil" <anon...@discussions.microsoft.com> wrote in message
news:96212747-93F8-4AEB...@microsoft.com...

Saravana [MVP]

unread,
Mar 16, 2004, 9:13:54 AM3/16/04
to
For showing message box in ASP.NET, you need to use client side scripts
only. You can use registerstartupscript or registerclientscript method to
register your client scripts in your form. In the client script you can use
window.confirm to show message box with ok/cancel button.
Check out this free messagebox control which is available at following
location which you can download and use it. Check out this url's for more
details.

www.extremeexperts.com

or
http://www.microsoft.com/india/msdn/articles/119.aspx


--
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com

"bassil" <anon...@discussions.microsoft.com> wrote in message
news:96212747-93F8-4AEB...@microsoft.com...

Kevin Spencer

unread,
Mar 16, 2004, 9:18:26 AM3/16/04
to
/// <summary>
/// Adds a JavaScript "alert()" with strMessage to Page Startup
/// </summary>
/// <param name="strMessage">Message to display</param>
public static void MsgBox(string strMessage)
{
StringBuilder s;
System.Web.UI.Page p;
if (HttpContext.Current == null) return;
s = new StringBuilder("<script type=\"text/javascript\">" +
Environment.NewLine +
"<!--" + Environment.NewLine);
s.Append("alert('" + strMessage.Replace("\"", "\\\"") + "');" +
Environment.NewLine + "// --></script>");
p = (System.Web.UI.Page) HttpContext.Current.Handler;
if (!(p.IsStartupScriptRegistered("MsgBox")))
p.RegisterStartupScript("MsgBox", s.ToString());
}

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Gönen EREN" <ge...@fenixidea.com> wrote in message
news:evlFCXyC...@TK2MSFTNGP09.phx.gbl...

0 new messages