I’ve just started working through a ASP.NET C# guide to assist me with an internship I’m starting next week. I’m familiar with OO, but never used C# or ASP.NET. In the beginning of this tutorial, I’ve created a simple webpage that is supposed to analyze the interest from an IRA account. There are two buttons, which are tagged with event handlers that refer to the C# code-behind file. I’m receiving the following errors from the browser, after compiling and running the program:
C:\Users\Kyle\Documents\Visual Studio 2010\WebSites\WebSite2\Default.aspx(49) : error BC30456: ‘btnCalculate_click’ is not a member of ‘ASP.default_aspx’.
AddHandler __ctrl.Click, AddressOf Me.btnCalculate_click
~~~~~~~~~~~~~~~~~~~~~
C:\Users\Kyle\Documents\Visual Studio 2010\WebSites\WebSite2\Default.aspx(52) : error BC30456: ‘btnClear_Click’ is not a member of ‘ASP.default_aspx’.
AddHandler __ctrl.Click, AddressOf Me.btnClear_Click
~~~~~~~~~~~~~~~~~
Here is my .aspx file:
<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>
And here is my code-behind file:
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 = "";
}
}
Could someone please let me know why these errors are popping up? I’ve retyped both of these from the source. Could there be a setting that I might need to have changed? I’ve never used VS2010, so I’m not 100% familiar with it, either.
Thanks in advance!
In your ASPX file I do not see your
@PageandCodeFiledeclarations.Based on your question it would look something like this:
Also, you should might want to read up on partial classes since this is how the code-behind model works:
http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.100).aspx