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
<asp:TextBox ID="XcoordTextbox" runat="server"></asp:TextBox>
<br />
Y Coord
<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
}
}
}
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.
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;
}
}