1

I am making a Login Page with ASP.Net using C#... and i have put my html code in Login.aspx page . that contains a textbox named username but when I give reference of my textbox in Login.aspx.cs to make the validations on this text box ... as

if(string.IsNullOrEmpty(username.Text))
{
...
...
}

i am getting an error that is saying ...-"The 'username' doesn't exist in the current context"...... how can i get rid of this error .... it is making me mas .... Help will be appreciable towards this error ... please help me ....

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %> 
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> </asp:Content> 
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
Tieson T.
  • 20,774
  • 6
  • 77
  • 92
Will
  • 43
  • 1
  • 7
  • Problem seems to be with you page directive have a look at that – yogi May 02 '13 at 05:18
  • 1
    You need to show more of your code if you want any help. Where would username be declared for example? – Karl-Johan Sjögren May 02 '13 at 05:18
  • Please add your code of Login.aspx and Login.aspx.cs – mmpatel009 May 02 '13 at 05:19
  • 1
    Probably you missed the `runat="server"` attribute in your username textbox – noobob May 02 '13 at 05:19
  • 1
    Probably duplicate of this : [The name 'controlname' does not exist in the current context](http://stackoverflow.com/questions/706603/the-name-controlname-does-not-exist-in-the-current-context) – Maryam Arshi May 02 '13 at 05:19
  • the initial code in Login.aspx is <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %> .. – Will May 02 '13 at 05:22
  • 1
    @Will edit your question. if you want to put some code – noobob May 02 '13 at 05:24
  • @Will I added the code from your comment to your original question. If that is truly the markup you have, you have no `username` control. – Tieson T. May 02 '13 at 05:28

1 Answers1

2

It depends on what you have taken.

If you have draged and droped ASP TextBox the automatically code has generated as:

<asp:TextBox ID="username" runat="server"></asp:TextBox>

which automatically contains runat="server"

But if you have taken HTML text box then code is as follows:

<input id="username" type="text" />

In this you will have to add the line on your own as runat="server"

It will look as follows:

<input id="username" type="text"  runat="server"/>

Then you can use it for serverside.

Hope its helpful.

Freelancer
  • 9,008
  • 7
  • 42
  • 81
  • Thanx...... Buddy .... it really helped me .... Thankz a lot ..... I am just a Beginner with you .... again thanx a lot – Will May 02 '13 at 05:36
  • @Will if answer is helpful, then you can accept this as a answer by ticking the checkbox. – Freelancer May 02 '13 at 05:41