我刚刚开始阅读 ASP.NET C# 指南,以帮助我完成下周开始的实习。我熟悉 OO,但从未使用过 C# 或 ASP.NET。在本教程的开头,我创建了一个简单的网页,用于分析 IRA 帐户的兴趣。有两个按钮,它们使用引用 C# 代码隐藏文件的事件处理程序进行标记。编译并运行程序后,我从浏览器收到以下错误:
C:\Users\Kyle\Documents\Visual Studio 2010\WebSites\WebSite2\Default.aspx(49):错误 BC30456:“btnCalculate_click”不是“ASP.default_aspx”的成员。
AddHandler __ctrl.Click, AddressOf Me.btnCalculate_click
~~~~~~~~~~~~~~~~~~~~~
C:\Users\Kyle\Documents\Visual Studio 2010\WebSites\WebSite2\Default.aspx(52):错误 BC30456:“btnClear_Click”不是“ASP.default_aspx”的成员。
AddHandler __ctrl.Click, AddressOf Me.btnClear_Click
~~~~~~~~~~~~~~~~~
这是我的 .aspx 文件:
<xmlns="http://www.w3.org/1999/xhtml">
<html>
<head runat="server">
<title>Chapter 2: Future Value</title>
<style type ="text/css">
.style1
{
color: #0000FF;
font-size: x-large;
}
.style2
{
width:100%;
}
.style3
{
width: 140px;
height: 23px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<span class="style1"><strong>401K Future Value</strong></span> <br /> <br />
<table class = "style2">
<tr>
<td class="style3">Montly investment</td>
<td><asp:DropDownList ID="ddlMonthlyInvestment" runat="server" Width="106px"></asp:DropDownList></td>
</tr>
<tr>
<td class="style3">Annual interest rate</td>
<td><asp:TextBox ID="txtInterestRate" runat="server" Width="100px">6.0</asp:TextBox></td>
</tr>
<tr>
<td class="style3">Number of years</td>
<td><asp:TextBox ID="txtYears" runat="server" Width="100px">10</asp:TextBox></td>
</tr>
<tr>
<td class="style3">Future Value</td>
<td><asp:Label ID="lblFutureValue" runat="server" Font-Bold="true"></asp:Label></td>
</tr>
<tr>
<td class="style3">
<asp:Button ID="Calculate" runat="server" onclick="btnCalculate_click" Text="Calculate" />
</td>
<td>
<asp:Button ID="Clear" runat="server" onclick="btnClear_Click" Text="Clear" />
</td>
</tr>
</table>
</div></form>
</body>
</html>
这是我的代码隐藏文件:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
for (int i = 50; i <= 500; i += 50)
ddlMonthlyInvestment.Items.Add(i.ToString());
}
protected void btnCaluculate_Click(object sender, EventArgs e)
{
if (IsValid)
{
int monthlyInvestment = Convert.ToInt32(ddlMonthlyInvestment.SelectedValue);
decimal yearlyInterestRate = Convert.ToDecimal(txtInterestRate.Text);
int years = Convert.ToInt32(txtYears.Text);
int months = years * 12;
decimal monthlyInterestRate = yearlyInterestRate / 12 / 100;
decimal futureValue = this.CalculateFutureValue(monthlyInvestment, monthlyInterestRate, months);
lblFutureValue.Text = futureValue.ToString("c");
}
}
protected decimal CalculateFutureValue(int monthlyInvestment, decimal monthlyInterestRate, int months)
{
decimal futureValue = 0;
for (int i = 0; i < months; i++)
{
futureValue = (futureValue + monthlyInvestment) * (1 + monthlyInterestRate);
}
return futureValue;
}
protected void btnClear_Click(object sender, EventArgs e)
{
ddlMonthlyInvestment.SelectedIndex = 0;
txtInterestRate.Text = "";
txtYears.Text = "";
lblFutureValue.Text = "";
}
}
有人可以让我知道为什么会出现这些错误吗?我已经从源头重新输入了这两个。是否有我可能需要更改的设置?我从未使用过 VS2010,所以我也不是 100% 熟悉它。
提前致谢!