Reputation: 13
I'm trying to a real basic collection of information from a website I'm building and it's failing and I can't figure out why.
The basics are I have a text field called "txtUserName" in the aspx file, so I should be able to just call txtUserName.Text in the aspx.cs file. But it says that the name 'txtUserName' does not exist in the current context.
Here's the code.
Default.aspx has this code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
User Name:
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><asp:RequiredFieldValidator ForeColor="Red" ID="RequiredFieldValidator2" ControlToValidate="txtUserName" runat="server" ErrorMessage="* Required"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<asp:TextBox ID="txtPWD" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="txtPWD" ID="RequiredFieldValidator1" ForeColor="Red" runat="server" ErrorMessage="* Required"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Login" OnClick="btnSubmit_Click" />
<asp:Button ID="bttnRegister" runat="server" Text="Register" onClick="btnRegister_Click"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Default.aspx.cs has this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
using System.Security.Cryptography;
public partial class Default : System.Web.UI.Page
{
string myConnectionString = ConfigurationManager.ConnectionStrings["External"].ToString();
public SqlConnection createConnection()
{
SqlConnection myConnection = new SqlConnection(myConnectionString);
myConnection.Open();
return myConnection;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
txtUserName.Text = "test";
}
Upvotes: 1
Views: 1802
Reputation: 18127
Click right button on the aspx and click view designer. After that the txtUserName
should be visible in the code behind. From time to time this bug happen, other solution is to add manually the textbox to the designer.cs file.
Upvotes: 1