My asp.net code is not detecting the textboxes in LoginView control

117 views Asked by At

Firstly, I am new to asp.net so please be patient with me. I have
created a Login control in the .aspx page. I am trying to access the username textbox called 'UserName' and the password textbox called
'Password', but when I build the application, I get the following errors:

Login does not contain a definition for 'Username'
Login does not contain a definition for 'Password'

The textboxes both have the correct id values, so I cannot understand
why asp.net is not locating the textbox controls. Please help!!

The following code for the Login.aspx page is:


    <%@ Page Language="C#" MasterPageFile="~/Template.master"    
    AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" 
    Title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent"   
    Runat="Server">


    <asp:LoginView ID="LoginView1" runat="server">
    <AnonymousTemplate>
    <asp:Login ID="Login1" runat="server" Width="50%"
    OnAuthenticate="Login1_Authenticate" >
    <LayoutTemplate>
    <table border="1px solid yellow" cellpadding="0" cellspacing="0"
      width="50%">
       <tr>
         <td style="height: 24px"><asp:Label runat="server" ID="lblUserName" 
            AssociatedControlID="UserName" Text="Email:"  /></td>
         <td style="height: 24px"><asp:TextBox id="UserName" 
          runat="server" 
           Width="55%"  /></td>
         <td style="height: 24px"></td>
       </tr>
       <tr>
         <td><asp:Label runat="server" ID="lblPassword" 
             AssociatedControlID="Password" Text="Password:"  />
         </td>
        <td><asp:TextBox ID="Password" runat="server"   
         TextMode="Password"  
         Width="95%"  /></td>
      </tr>
    </table>
    <table border="0" cellpadding="0" cellspacing="0" width="80%" 
      style="border: solid 1px red";>
      <tr>
     <td><asp:CheckBox ID="RememberMe" CssClass="chkrememberMe"  
     runat="server" Text="Remember me" >
     </asp:CheckBox>
     </td>
    <td>&nbsp;</td>
   </tr>
  </table>

   <asp:HyperLink ID="lnkRegister" CssClass="linkButtonReg"    
   runat="server"     
       NavigateUrl="~/Register.aspx">Create new account

    </asp:HyperLink><br />

    <asp:HyperLink ID="lnkPasswordRecovery" CssClass="linkButtonPass"   
    runat="server" NavigateUrl="~/PasswordRecovery.aspx" >I forgot my    
    password
    </asp:HyperLink>
    </div>

    </LayoutTemplate>
    </asp:Login>
    </AnonymousTemplate>

    </asp:LoginView>

    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="RightContent"     
    Runat="Server">
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="LeftContent"    
    Runat="Server">
    </asp:Content>

The code behind file for The Login.aspx.cs is:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
  public partial class Login : System.Web.UI.Page
  {
    string userEmail = "";
    string userName = "";
    string password = "";

    protected void Page_Load(object sender, EventArgs e)
    {

    }  

     protected void Login1_Authenticate(object sender, 
      AuthenticateEventArgs e)
       {

        Login Login1 = ((Login)LoginView1.FindControl("Login1"));
        userEmail = Login1.UserName.ToString();
        userName = Membership.GetUserNameByEmail(userEmail);
        password = Login1.Password.ToString();

        if (Membership.ValidateUser(userName, password))
        {
            e.Authenticated = true;
            Login1.UserName = userName;
        }
        else
        {

            e.Authenticated = false;

        }

    }
  }
0

There are 0 answers