PN8
PN8

Reputation: 11

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>

Upvotes: 1

Views: 179

Answers (2)

UmarKashmiri
UmarKashmiri

Reputation: 872

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.

Upvotes: 1

Amarnath R Shenoy
Amarnath R Shenoy

Reputation: 5201

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

Upvotes: 0

Related Questions