i am learning javascript client side scripting language and i am using js in ASP.. i dont know when i pile 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" ".dtd">
<html xmlns="" >
<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>
i am learning javascript client side scripting language and i am using js in ASP.. i dont know when i pile 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/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/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>
Share
Improve this question
edited Feb 13, 2011 at 8:36
Colin Pickard
46.7k13 gold badges104 silver badges151 bronze badges
asked Feb 13, 2011 at 8:33
Abid AliAbid Ali
1,7579 gold badges26 silver badges62 bronze badges
2
- can somebody edit the code :/.. please.. – Abid Ali Commented Feb 13, 2011 at 8:34
- i dont know how to edit.. :/ sorry – Abid Ali Commented Feb 13, 2011 at 8:35
3 Answers
Reset to default 5You'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;" />
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.
OnClick
- is click handler on server side. If you want to call javascript function, use OnClientClick
.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745275831a4620033.html
评论列表(0条)