1

I’m trying to write a basic ASP.NET form (using Visual Studio 2010) to submit numbers to a database. Because this is a very early attempt for me to use ASP.NET, I wrote it very simply at first and now I am trying to add the remaining features. Originally, everything was all inline code. Now I am trying to switch it to use a codebehind page, but I cannot get it to resolve the names of my textboxes from within my codebehind file. I built these pages while following some tutorials, and have looked at several other sources to try to fix this, but everything I’ve seen so far seems to indicate that I have it all set for a codebehind, yet it doesn’t work. VS gives errors during compile, stating the textbox names do not exist in the current context. When I remove all references to the textboxes, the page works and my button event fires. For grins I added references to the textboxes in the codebehind, like so protected TextBox XcoordTextbox; (like I believe you would in ASP.NET 1.0) but then I get a runtime error CS0102: The type 'ManualEntry.default_aspx' already contains a definition for 'XcoordTextbox' Below are the significant portions of my code. Can you help explain why this doesn’t work?

ASPX file

<%@ Page    Language="C#" CodeFile="default.aspx.cs" Inherits="ManualEntry.default_aspx" AutoEventWireup="true" %>
<!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"></head>
<body>
    <form id="HtmlForm" runat="server">
        <div>
            X Coord&nbsp;
            <asp:TextBox ID="XcoordTextbox" runat="server"></asp:TextBox>
            <br />
            Y Coord&nbsp;
            <asp:TextBox ID="YcoordTextbox" runat="server"></asp:TextBox>
            <asp:Button ID="SubmitButton" runat="server" onclick="SubmitButton_Click" Text="Submit" />
        </div>
    </form>
</body>
</html>

Codebehind file

using System;
using System.Web.UI;

namespace ManualEntry
{
    public partial class default_aspx : Page
    {
        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            var Xcoord = XcoordTextbox.Text;
            var Ycoord = YcoordTextbox.Text;
            //More Code Here
        }
    }
}
techturtle
  • 2,519
  • 5
  • 28
  • 54
  • Worst case, you should be able to do `this.FindControl("XcoordTextbox") as TextBox` to find your controls. – Tejs May 15 '12 at 17:06
  • Did you try cleaning your project? (Right click on the project in Solution Explorer and choose Clean) – Mark May 15 '12 at 17:07
  • @Mark - I have tried Cleaning it, but it doesn't seem to affect anything. – techturtle May 15 '12 at 17:43
  • what is the cs file name of default_aspx class? is it default.aspx.cs? – Damith May 15 '12 at 18:05
  • @UnhandledException Yes. The file names are default.aspx for the page and default.aspx.cs for the codebehind. The class name should be irrelevant as long as it matches the `Inherits` property on the ASPX page, right? – techturtle May 15 '12 at 18:12
  • @Tejs - So that did work. I guess, as you say, I have my "worst case" option and can proceed if necessary. Still, I would like to know what the heck is wrong since everything I've read so far says my original way should be working... – techturtle May 15 '12 at 18:15

8 Answers8

1

go to Microsfot.NET[.NET version]\Temporary ASP.NET Files. and delete temparary files for your web site.

e.g for .net 4.0

C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files

if you use CodeFile directive then compiled files goes to Temporary ASP.NET Files

And also delete bin and object folders from your solution and rebuild the web application.

EDIT

your default.aspx.designer.cs should look like below, don't add controls to this page manually.

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated. 
// </auto-generated>
//------------------------------------------------------------------------------

namespace ManualEntry {


    public partial class default_aspx {

        /// <summary>
        /// Head1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.HtmlControls.HtmlHead Head1;

        /// <summary>
        /// HtmlForm control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.HtmlControls.HtmlForm HtmlForm;

        /// <summary>
        /// XcoordTextbox control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.TextBox XcoordTextbox;

        /// <summary>
        /// YcoordTextbox control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.TextBox YcoordTextbox;

        /// <summary>
        /// SubmitButton control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.Button SubmitButton;
    }
}
Damith
  • 62,401
  • 13
  • 102
  • 153
  • This seems like a manual way to do the "Clean Solution" already mentioned. I tried it anyway, but no change. – techturtle May 15 '12 at 18:02
  • Regarding your edit... I don't seem to have a `default.aspx.designer.cs` page. That sounds like something that could be causing behavior like this, but I don't know how they are added to a project or why I don't have one. EDIT: I found this link [aspx.designer.cs how does it work?](http://stackoverflow.com/questions/3284710/aspx-designer-cs-how-does-it-work) which includes information about regenerating the designer.cs file. It appears to be working now. – techturtle May 15 '12 at 19:07
1

I have this problem from time to time. I usually do the followwing:

  1. Go to default.aspx.designer.cs.
  2. Make sure the namespace and class name are correct.
  3. remove everything inside the default_aspx class.
  4. Save and close default.aspx.designer.cs.
  5. Make a change (add a space or something) to default.aspx.
  6. Recompile the project.

It could also be your naming convention, try changing default_aspx to default_aspx1. I have seen default_aspx used by .NET.
Also, in my .NET 4.0 the default is to use CodeBehind instead of CodeFile. This seems to be the intended use case for all but .NET 1.1. See also CodeFile vs CodeBehind

If all else fails, try creating the page again from scratch.

Community
  • 1
  • 1
Trisped
  • 5,705
  • 2
  • 45
  • 58
0

your CodeFile="default.aspx.cs" statement is wrong

jmakki
  • 16
  • 1
0

It should be something like

CodeFile="default_aspx.aspx.cs" Inherits="default_aspx"
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • If you notice my first block of code, I do have both of these, with the exception that my `Inherits` property includes the namespace qualifier. – techturtle May 15 '12 at 17:38
  • NO, in your aspx you mention `CodeFile="default.aspx.cs"` and in cs `public partial class default_aspx`. They should be same and not different. Also, `Inherits` namespace not needed. – Rahul May 15 '12 at 17:41
  • The `CodeFile` entry specifies the **file name** while the `public partial class` is the **class name**. They are not the same. I previously had different names before and it didn't work. Just in case it was the names, I got these specific names from one of the tutorials I watched and it seemed to work OK for them. Also, the `Inherits` property does require the namespace declaration because VS identifies it as an error if you don't include it ("cannot resolve symbol"). – techturtle May 15 '12 at 18:09
0

use

CodeBehind="default.aspx.cs"
user1396468
  • 83
  • 1
  • 1
  • 4
0

Your codefile tag in your asp must be the same of the class name. Try something like real class names. HelloWord.aspx or something.

Taryn
  • 242,637
  • 56
  • 362
  • 405
Jaime Yule
  • 981
  • 1
  • 11
  • 20
  • As I mentioned in another comment, codefile and class names are separate. From what I have seen it is only convention that says they should be the same. Still I tried it. Currently my Page is **MyPage.aspx**, codebehind is **MyPage.aspx.cs**, class is `public partial class MyPage : Page` and the page includes `Inherits="ManualEntry.MyPage` and `CodeBehind="MyPage.aspx.cs"`. It still gives the exact same errors. – techturtle May 15 '12 at 18:30
0

try changing your inherits to be Inherits="ManualEntry.default" instead of Inherits="ManualEntry.default_aspx"

Mark
  • 1,455
  • 3
  • 28
  • 51
0

Go To your .aspx file and Re-check your Header like <%@............%> there should be a link to your code behind file name which is .cs file like......

Checkout this 1....:)

{<%@ Page Language="C#" AutoEventWireup="true" **CodeBehind="Home.aspx.cs"** Inherits="MyCompany.Home" MasterPageFile="~/Site1.Master"%>}
Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75