Compilation Error: CS1061

Compilation Error: CS1061

我知道这个网站上几乎没有非常相似的答案,但我无法弄清楚我在这里做错了什么。完整的错误信息在这里:

“/”应用程序中的服务器错误。

编译错误

说明:编译服务此请求所需的资源时出错。请查看以下特定错误详细信息并适当修改您的源代码。

编译器错误消息:CS1061:'ASP.webform1_aspx' 不包含 'addNumbers' 的定义,并且找不到接受类型 'ASP.webform1_aspx' 的第一个参数的扩展方法 'addNumbers' (您是否缺少 using 指令或程序集引用?)

来源错误:

第 37 行: 第 38 行:

Line 39:

第 40 行: 第 41 行:

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

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script>
            function addNumbers()
            {
                var firstNumber = parseFloat(document.getElementById("TextBox1").value);
                var secondNumber = parseFloat(document.getElementById("TextBox2").value);
                document.getElementById("TextBox3").value = firstNumber + secondNumber; 
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <table>
            <tr>
                <td>First Number: </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Second Number: </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Result: </td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Add" OnClick="addNumbers()" /></td>
            </tr>
        </table>
        </div>
        </form>
    </body>
    </html>

代码来自 aspx.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;



    namespace WebApplication2
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {

            }
        }
    }

这个:

<asp:Button ID="Button1" runat="server" Text="Add" OnClick="addNumbers()" />

要求您在代码隐藏文件中有 OnClick 方法。我认为您需要普通按钮,而不是服务器端按钮:

<button onclick="addNumbers()">Add</button>