How to pop a message box in ASP.NET page

  • Thread starter Thread starter kai
  • Start date Start date
K

kai

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
 
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.)
 
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
 
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
 
Hi, Bob
It works!!! Thanks


Kai
Bob Lehmann said:
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
 
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...
 
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
 
/// <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.
 
Back
Top