我剛剛開始通過ASP.NET C#指南開展工作,以幫助我實習下週開始的實習。我熟悉面向對象,但從未使用過C#或ASP.NET。在本教程開始時,我創建了一個簡單的網頁,用於分析IRA帳戶的興趣。有兩個按鈕,它們標記有引用C#代碼隱藏文件的事件處理程序。在編譯和運行程序後,我收到以下來自瀏覽器的錯誤:單頁ASP.NET網站錯誤BC30456
C:\ Users \ Kyle \ Documents \ Visual Studio 2010 \ WebSites \ WebSite2 \ Default.aspx(49):error BC30456:' 「btnCalculate_click」不是「ASP.default_aspx」的成員。
AddHandler __ctrl.Click, AddressOf Me.btnCalculate_click
~~~~~~~~~~~~~~~~~~~~~
C:\用戶\凱爾\文件\的Visual Studio 2010 \網站已\ 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%熟悉它。
在此先感謝!
['AddHandler'](http://msdn.microsoft.com/en-us/library/7taxzxka%28v=vs.100%29.aspx)是VB.NET。這是添加事件處理程序的VB方式,C#使用'+ ='運算符。 –