3

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>
2
  • can somebody edit the code :/.. please.. Commented Feb 13, 2011 at 8:34
  • i dont know how to edit.. :/ sorry Commented Feb 13, 2011 at 8:35

3 Answers 3

5

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;" />
Sign up to request clarification or add additional context in comments.

5 Comments

what do i do if i wish to write the same code on server side?
@Abid, what do you intend to accomplish by showing a modal dialog box on the server? :)
umm.. :) i dont know.. i was just trying things without thinking what i want to do actualy :)
actually, i am a newbie.. i just try and try and try.. i dont exactly know what to do..
@Abid, well, you could handle the click event server-side (using OnClick), but then popup() should be written in C#, and should register a script to call alert() on the client when the page has finished loading. Check out RegisterClientScriptBlock() for the gory details.
1

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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.