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 he开发者_StackOverflow中文版re");
if (username) {
alert("nice to meet you," + username + ".");
}
}
is there any way to call javascript function on asp button?
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()" />
Change this:
onclick="rock()"
To:
onclientclick="rock()"
Depending on your code, you may need to set CausesPostback = false;
精彩评论