Abid Ali
Abid Ali

Reputation: 1747

JavaScript into Asp Page

i am learning javascript client side scripting language and i am using js in ASP.. i dont know when i compile the code, it shows

COMPILATION ERROR:Compiler Error Message: CS1061: 'ASP.default_aspx' does not contain a definition for 'popup' and no extension method 'popup' accepting a first argument of type 'ASP.default_aspx' could be found (are you missing a using directive or an assembly reference?)

here is my code :

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WEB_test_app._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Shuraat Web ki</title>
    <script type ="text/javascript">
    function popup()
    {
    alert("popup!!");
    }    
    </script> 
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick ="popup()"/>

      <script type ="text/javascript">
      document.write("Abid");
      </script>   
    </div>
    </form>
</body>
</html>

Upvotes: 3

Views: 2616

Answers (3)

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 262919

You're confusing client-side and server-side events. The popup() function is written in Javascript and runs on the client, but OnClick is a server-side event, so its handler runs on the server and has to be written in the language of your page (C#). popup() has no meaning in that context.

You can use the ClientClick event in order to handle the button click client-side. Note that you probably should return false from the handler in order to avoid a postback to the server:

<asp:Button ID="Button1" runat="server" Text="Button"
    OnClientClick ="popup(); return false;" />

Upvotes: 5

gor
gor

Reputation: 11658

OnClick - is click handler on server side. If you want to call javascript function, use OnClientClick.

Upvotes: 1

Ken D
Ken D

Reputation: 5968

You have to assign popup() to the event OnClientClick instead of Click like this:

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick ="popup()"/>

The reasoning behind your problem is that when the run-time saw OnClick it tried to attatch that event with a C#/VB.Net (Code-Behind) method (which obviously does not exist) and we solved that by attaching the event OnClientClick to your Javascript method.

Upvotes: 1

Related Questions