1

this code is a excerpt from the book asp.net all in one reference for dummies on pages 18 and 19. The problem with this is that it doesn't compile. The compiler says txtFirst, txtSecond and lblAnswer are not found in the current context. I am new in this field, could someone please help me out ?

<%@ 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();
        }
    }
}
th1rdey3
  • 4,176
  • 7
  • 30
  • 66
crowso
  • 2,049
  • 9
  • 33
  • 38
  • Try checking out the answers to [this question](http://stackoverflow.com/questions/1316757/cant-access-control-id-in-code-behind). – Damir Arh Oct 07 '12 at 06:34

5 Answers5

1

Try this version instead - I've tested it and it works:

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>

CODE-BEHIND:

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();
        }
    }
}
IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
  • wow you saved my life. How frustrating C# can be at times :(. Who would have thought that replacing CodeFile with CodeBehind will resolve this issue. – crowso Oct 07 '12 at 06:43
  • 1
    See here: http://msdn.microsoft.com/en-us/library/ydy4x04a(v=vs.100).aspx CodeFile is for Website Projects whereas Codebehind is for Web Application Projects. – IrishChieftain Oct 07 '12 at 06:45
0

you must put your c# code in code behind page in solution explorer of Visual studio find Default.aspx.cs and copy

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();
        }
    }
}

to the file I hope this help you

danarj
  • 1,798
  • 7
  • 26
  • 54
  • change Inherits="_Default" to Inherits="Namespace.Default" namespace is your project namespace like this in my situation Inherits="WebApplication1.WebForm1" – danarj Oct 07 '12 at 06:32
  • change Inherits="_Default" to Inherits="WebApplication2.Default" – danarj Oct 07 '12 at 06:36
  • your namespace is Test so it should be like this Inherits="Test.Default" – danarj Oct 07 '12 at 06:43
0

if you don't want to use separate .cs file then you have to use script tag in your aspx page.

<%@ 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>
th1rdey3
  • 4,176
  • 7
  • 30
  • 66
0

Make two changes

  1. In your aspx page, change to Inherits="WebApplication2.Default"
  2. In your code behind change the class name to Default instead of _Default.

Then rebuild the solution.

Another reason could be copy-paste problem which sometimes messes up the designer file. Try deleting the controls and re-adding them manually.

Ashwin Singh
  • 7,197
  • 4
  • 36
  • 55
0

Are you making a web site or a web application? If the latter, then try deleting (or removing/renaming, if you prefer to back it up first) the Default.aspx.cs file. Then right-click the Default.aspx file in Solution Explorer and select "Convert to Web Application".

Kenneth K.
  • 2,987
  • 1
  • 23
  • 30