如何调用类方法asp.net C#

时间:2012-10-07 06:19:47

标签: c# asp.net

这段代码摘自第186页和第19页的asp.net一书中的dummies参考。这个问题是它不能编译。编译器说在当前上下文中找不到txtFirst,txtSecond和lblAnswer。我是这个领域的新手,有人可以帮帮我吗?

<%@ Page Language="C#" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_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 id="Head1" runat="server">
    <title>Simple Calculator</title>
</head>
<body>
    <form id="form2" runat="server">
        <div>
            <h1>The Simple Calculator</h1>
            First number:
            <asp:TextBox ID="txtFirst" runat="server" />
            <br />
            <br />
            Second number:
            <asp:TextBox ID="txtSecond" runat="server" />
            <br />
            <br />
            <asp:Button ID="btnAdd" runat="server"
                OnClick="btnAdd_Click" Text="Add" />
            <br />
            <br />
            The answer is:
            <asp:Label ID="lblAnswer" runat="server" />
        </div>
    </form>
</body>
</html>



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace WebApplication2
{
    public partial class _Default : System.Web.UI.Page
    {

        protected void btnAdd_Click(object sender,EventArgs e)
        {
            decimal a = decimal.Parse(txtFirst.Text);
            decimal b = decimal.Parse(txtSecond.Text);
            decimal c = a + b;
            lblAnswer.Text = c.ToString();
        }
    }
}

5 个答案:

答案 0 :(得分:1)

尝试使用此版本 - 我已对其进行了测试并且可以正常运行:

<强> ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
    Inherits="Test.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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>The Simple Calculator</h1>
            First number:
            <asp:TextBox ID="txtFirst" runat="server" />
            <br />
            <br />
            Second number:
            <asp:TextBox ID="txtSecond" runat="server" />
            <br />
            <br />
            <asp:Button ID="btnAdd" runat="server"
                OnClick="btnAdd_Click" Text="Add" />
            <br />
            <br />
            The answer is:
            <asp:Label ID="lblAnswer" runat="server" />
    </div>
    </form>
</body>
</html>

<强>代码隐藏:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


namespace Test
{
    public partial class Default : System.Web.UI.Page
    {
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            decimal a = decimal.Parse(txtFirst.Text);
            decimal b = decimal.Parse(txtSecond.Text);
            decimal c = a + b;
            lblAnswer.Text = c.ToString();
        }
    }
}

答案 1 :(得分:0)

你必须将你的c#代码放在Visual Studio解决方案资源管理器中的代码页面后找到Default.aspx.cs并复制

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace WebApplication2
{
    public partial class _Default : System.Web.UI.Page
    {

        protected void btnAdd_Click(object sender,EventArgs e)
        {
            decimal a = decimal.Parse(txtFirst.Text);
            decimal b = decimal.Parse(txtSecond.Text);
            decimal c = a + b;
            lblAnswer.Text = c.ToString();
        }
    }
}

到文件我希望这能帮到你

答案 2 :(得分:0)

如果您不想使用单独的.cs文件,则必须在aspx页面中使用脚本标记。

<%@ Page Language="C#">

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

<script runat="server">
    protected void btnAdd_Click(object sender,EventArgs e)
    {
        decimal a = decimal.Parse(txtFirst.Text);
        decimal b = decimal.Parse(txtSecond.Text);
        decimal c = a + b;
        lblAnswer.Text = c.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Simple Calculator</title>
</head>
<body>
    <form id="form2" runat="server">
        <div>
            <h1>The Simple Calculator</h1>
            First number:
            <asp:TextBox ID="txtFirst" runat="server" />
            <br />
            <br />
            Second number:
            <asp:TextBox ID="txtSecond" runat="server" />
            <br />
            <br />
            <asp:Button ID="btnAdd" runat="server"
                OnClick="btnAdd_Click" Text="Add" />
            <br />
            <br />
            The answer is:
            <asp:Label ID="lblAnswer" runat="server" />
        </div>
    </form>
</body>
</html>

答案 3 :(得分:0)

进行两项更改

  1. 在您的aspx页面中,更改为Inherits="WebApplication2.Default"
  2. 在您的代码中,将班级名称更改为Default而不是_Default
  3. 然后重建解决方案。

    另一个原因可能是复制粘贴问题,有时会弄乱设计器文件。尝试删除控件并手动重新添加它们。

答案 4 :(得分:0)

您是在制作网站还是网络应用程序?如果是后者,则尝试删除(或删除/重命名,如果您更喜欢先备份它)Default.aspx.cs文件。然后在解决方案资源管理器中右键单击Default.aspx文件,并选择“转换为Web应用程序”。