1
votes

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>
2
your asp:button is missing "<" - Zaki
@Zaki, that was a typo, i have actually used : <asp:Button id="Button" onclick="myFunction()" runat="server" Text="Button" /> - PN8

2 Answers

1
votes

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.