2

这就是我正在尝试的。调用内联js的函数。但我没有工作。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="javascript_Tutorials.WebForm1" %>

    <!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>javaScript Tutorials</title>
        <script type="text/javascript" language="javascript">
            function objects() {
                alert("Well come");
            }
        </script>

    </head>
    <body>
        <form runat="server" id="myform">
        <asp:Button onclick="<% objects(); %>" runat="server"  />
        </form>

    </body>
    </html>

并出现以下错误:

CS1061:“ASP.webform1_aspx”不包含“对象”的定义,并且找不到接受“ASP.webform1_aspx”类型的第一个参数的扩展方法“对象”(您是否缺少 using 指令或程序集引用?)

4

4 回答 4

3

改为使用Button.OnClientClickonclick来注册客户端 javascript 事件并为按钮分配一些 id。onclick用于注册服务器端事件。

   <asp:Button  id="yourButtonID"  runat="server" OnClientClick="objects();" />
于 2013-03-21T11:02:07.633 回答
1

试试这个>>

<!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>javaScript Tutorials</title>

</head>
<body>
    <form runat="server" id="myform">
    <asp:Button onclick="<% objects(); %>" runat="server"  />
    </form>

</body>
</html>
<script type="text/javascript" language="javascript">
            function objects() {
                alert("Well come");
            }
        </script>

当上述代码不起作用时,最后粘贴脚本也有效。

于 2013-03-21T11:02:21.463 回答
0
Remove Parenthesis after function call

<asp:Button ID="Button1" OnClientClick="objects;" runat="server"  />
于 2013-03-21T11:17:42.313 回答
0

您可以使用以下代码:-

<asp:Button ID="Button1" OnClientClick="return objects();" runat="server"  />
于 2013-03-21T11:04:37.007 回答