How to pop a message box in ASP.NET page

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
 
S

Steven Cheng[MSFT]

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.)
 
K

kai

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
 
B

Bob Lehmann

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
 
K

kai

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top