Fire Javascript method on BUTTON click??!?

J

Jack Black

OK, I've got a basic webform set up, with several buttons that I want to
trigger different things on when they're clicked. For example, when the
onClick event is triggered on one, I want a javascript method to fire and
open a new window, redirecting to either another page on my site or a page
off-site.

1) How do I do this? A traditional "onclick" event on the control line
doesn't work, and setting up the CommandName/CommandArgument properties
doesn't work either.
2) How can I do something WITHOUT a button performing a SUBMIT action??
I've tried wiring up the CommandName/OnClick/OnCommand events, and they
either don't call the method (if in the same script) or I get an error
telling me the "name isn't defined" (if the method IS defined in a
code-behind page)...

Thanks!
Jack
 
K

Ken Cox [Microsoft MVP]

Could you post the code that isn't working for you? Creating a button that
responds to an onclick to open a window but doesn't cause a postback is
pretty easy, so there must be something more to it that we need to see.

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Button1.Attributes.Add _
("onclick", _
"window.open('http://msdn." & _
"microsoft.com/asp.net/')" & _
";return false;")
End Sub

<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
</form>
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top