SA.
SA.

Reputation: 752

javascript on asp button control

i am using asp button control to call a javascript function. but it is not working. it is showing an error i am calling javascript function as:

 <asp:button ID="Button1" runat="server" Text="Button" onclick="rock()" />

the javascript function is as:

function rock() {
   var username = prompt("what is your name?", "please enter your name here");

   if (username) {
       alert("nice to meet you," + username + ".");
    }
}

is there any way to call javascript function on asp button?

Upvotes: 0

Views: 328

Answers (2)

kemiller2002
kemiller2002

Reputation: 115488

Change this:

onclick="rock()"

To:

onclientclick="rock()"

Depending on your code, you may need to set CausesPostback = false;

Upvotes: 0

Tom Gullen
Tom Gullen

Reputation: 61727

onclick links to a server method, you want onclientclick. If you had a simple HTML control which was not a server control, you can use onclick but it has a different meaning when it's an ASP.net control.

Fixed code:

<asp:button ID="Button1" runat="server" Text="Button" onclientclick="rock()" />

Upvotes: 1

Related Questions