0

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?

2 Answers 2

1

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()" />
Sign up to request clarification or add additional context in comments.

2 Comments

it means that there are different events for every control in asp.how do i came to know about all of them..
@kawade with practise, you will get to know them all!
0

Change this:

onclick="rock()"

To:

onclientclick="rock()"

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.