开发者

javascript on asp button control

开发者 https://www.devze.com 2023-03-06 20:38 出处:网络
i am using asp button control to call a javascript function. but it is not working. it is showing an error

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;

0

精彩评论

暂无评论...
验证码 换一张
取 消