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
}
}
}