简体   繁体   中英

HTML server control vs ASP.net web

I am trying to call a javascript function on click of a button (server control).When i use html button control (with a runat="server" attribute) for the above purpose, am able to do it , but when I use ASP.net Button control (web control), It gives me the error: BC30456: 'myFunction' is not a member of 'ASP.webform1_aspx'.

What is the difference ?

For case 1 my code using html button control, case 2 is my code using ASP.net web control:

case 1:

<html>
     <head>
     <script >
     function myFunction()
     {
     alert("Hello World!");
     }
     </script>
     </head>

     <body>
     <button runat="server" onclick="myFunction()">Try it</button>
     </body>
     </html>

** case 2:**

<html>
     <head>
     <script >
     function myFunction()
     {
     alert("Hello World!");
     }
     </script>
     </head>

     <body>

    <asp:Button id="Button"  onclick="myFunction()" runat="server" Text="Button" />
     </body>
     </html>

When you write runat="server" it becomes the server control and to call JS function with server side control you have to use OnClientClick instead of OnClick method. If you use Onclick method it will try to find the function on server.

Hope this will help.

use onClientClick for asp control

Actually onClick searches a method in your aspx.cs file, ie a C# or VB function

http://forums.asp.net/t/1249580.aspx?onclick+versus+onclientclick

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick(v=vs.110).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM