I keep getting the error "The name "User Name" does not exist in the current context. As you can see "UserName" is clearly defined on my code behind page and I am referencing my page. So why does it say that it does not exist?
Here is my C# -
public partial class Account_Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}
protected void btnLogOn_Click(object sender, EventArgs e)
{
Session["SubName"] = UserName.Text;
}
}
Here is my HTML -
<p>
Please enter your username and password.
</p>
<form action="/Account/LogOn" method="post">
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
<label for="UserName">User name</label>
</div>
<div class="editor-field">
<input id="UserName" name="UserName" type="text" value="" />
</div>
<div class="editor-label">
<label for="Password">Password</label>
</div>
<div class="editor-field">
<input id="Password" name="Password" type="password" />
</div>
<p>
<input id="btnLogOn" type="submit" value="Log On" />
</p>
</fieldset>
</div>
</form>
</div>
Your input box needs to have
runat=server
if you want to access it from code behind.<input id="UserName" name="UserName" type="text" value="" runat=server />
Otherwise you will receive an error that UserName does not exist, as you have already mentioned.