1

I do not think my question is new, but I tried many solutions on this website and nothing worked. So, here is my problem:

I have this code that giving me 2 errors about "password" and "username". I created the project as a website, so I do not have a design file to fix. How can I fix this?

Here is the code from aspx file:

 <section id="loginForm">
        <h2>&nbsp;</h2>
        <asp:Login runat="server" ViewStateMode="Disabled" RenderOuterTable="false">
            <LayoutTemplate>
                <p class="validation-summary-errors">
                    &nbsp;</p>
                <fieldset>
                    <legend>Log in Form</legend>
                    <ol>
                        <li>
                            <asp:Label runat="server" AssociatedControlID="UserName">User name</asp:Label>
                            <asp:TextBox runat="server" ID="UserName" />
                            <asp:RequiredFieldValidator runat="server" ControlToValidate="UserName" CssClass="field-validation-error" ErrorMessage="The user name field is required." />
                        </li>
                        <li>
                            <asp:Label runat="server" AssociatedControlID="Password">Password</asp:Label>
                            <asp:TextBox runat="server" ID="Password" TextMode="Password" />
                            <asp:RequiredFieldValidator runat="server" ControlToValidate="Password" CssClass="field-validation-error" ErrorMessage="The password field is required." />
                        </li>
                        <li>
                        </li>
                    </ol>
                    <asp:Button runat="server" CommandName="Login" Text="Log in" ID="Login" OnClick="Login_Click" Width="300px" />
                </fieldset>
            </LayoutTemplate>
        </asp:Login>
        <p>
            &nbsp;</p>
    </section>

Here is the code in aspx.cs file:

 string UN = UserName.Text; //Request.QueryString["UserName"];
      string PW = Password.Text;//Request.QueryString["Password"];
Suha Alhabaj
  • 101
  • 2
  • 11
  • Possible duplicate of [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) – Alex Jul 12 '16 at 14:08
  • Is there any case that you copy-pasted these in? I've found that this can often cause the code-behind for the ASPX page to not be properly updated and thus render some of the controls inaccessible. You might try deleting them, manually creating them and saving / rebuilding to see if that makes a difference. – Rion Williams Jul 12 '16 at 14:09
  • I have not copy paste them, but I just deleted them and put new ones and no use. I got the same error. – Suha Alhabaj Jul 12 '16 at 14:14
  • Alex, I read this post before and i tried the solutions, none worked. Thanks though! – Suha Alhabaj Jul 12 '16 at 14:15
  • the Login control is not very intuitive. You must give it an ID and access the username and password as properties of the Login control. Even then you can only get the values during certain events. [See this](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login.password.aspx) – Crowcoder Jul 12 '16 at 14:16
  • I think this is what I did @Crowcoder. If not, how can I do that? – Suha Alhabaj Jul 12 '16 at 14:23
  • As @Crowcoder said you need to give your login control an Id. – Kami Jul 12 '16 at 14:24
  • i got this error: Validation (ASP.Net): Attribute 'RenderOuterTable' is not a valid attribute of element 'TextBox' – Suha Alhabaj Jul 12 '16 at 14:27
  • Your UserName and Password boxes are part of the Login Control as parent control. you cannot treat them as separate ASP.Net TextBox controls. – Kami Jul 12 '16 at 14:30
  • protected void Login_Click(object sender, EventArgs e) { string un = LoginBox.UserName; string pw = LoginBox.Password; } – Kami Jul 12 '16 at 14:31

2 Answers2

2

First you need a way to reference your Login control from code - so give it an ID:

 <asp:Login ID="MyLogin" runat="server" ViewStateMode="Disabled" RenderOuterTable="false">

Then, you access the username and password as properties of the control instance:

MyLogin.UserName
MyLogin.Password

Not as just regular text boxes like you are trying to do.

You can access these values in the LoggedIn event.

protected void MyLogin_LoggedIn(object sender, EventArgs e)
{
    string name = MyLogin.UserName;
}
Crowcoder
  • 11,250
  • 3
  • 36
  • 45
0

I haven't used anytime the login control, however since i see a ViewTemplate, i assume you need to find the controls within the login container, like the way you do in a Gridview.

control myControl=Login.findControl("id");
textbox password= (textbox) myControl;

that way it should be accesible, and by the way dont forget to use the runat="server" tag on the controls, if dont, they wont be accesible in code behind.

thepanch
  • 353
  • 2
  • 13