Domanda

HTML:

<body>
<form id="form1" runat="server">
<div>
    <asp:LoginView ID="LoginView1" runat="server">
        <AnonymousTemplate>
            <asp:Login ID="Login1" runat="server" OnLoginError="LoginError">
            </asp:Login>
        </AnonymousTemplate>          
        <LoggedInTemplate>
            Username:
            <asp:LoginName ID="LoginName1" runat="server" />
        </LoggedInTemplate>
    </asp:LoginView>
</div>
</form>

Code-behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void LoginError(object sender, EventArgs e)
{
    Login Login1 = (Login)LoginView1.FindControl("Login1");
    string userName= Login1.UserName;
    MembershipUser user = Membership.GetUser(userName);
    if (user == null)
    {
        Login1.FailureText = "Username Invalid";
    }
    else
    {
        if (!user.IsApproved)
        {
            Login1.FailureText = "User Not Yet Approved";
        }
        else if (user.IsLockedOut)
        {
            Login1.FailureText = "User Locked Out";
        }
    }
}
}

The above shows my code. When I try to run this, it gives me the following errors:

'Login' does not contain a definition for 'UserName' and no extension method 'UserName' accepting a first argument of type 'Login' could be found (are you missing a using directive or an assembly reference?)

'Login' does not contain a definition for 'FailureText' and no extension method 'FailureText' accepting a first argument of type 'Login' could be found (are you missing a using directive or an assembly reference?)

Note I have included the System.Web.UI.WebControls reference.

The code starts working, however, if I explicitly typecast the Login control:

System.Web.UI.WebControls.Login Login1 = (System.Web.UI.WebControls.Login)LoginView1.FindControl("Login1");

Can anyone please explain why this is happening? In the first case, if I hover over the Login keyword, it shows "class System.Web.UI.WebControls.Login" so it is not getting incorrectly referenced.

Thanks in advance. :)

È stato utile?

Soluzione

The most likely cause is another page or user-control called Login. Without a namespace prefix, the compiler will resolve Login to that page/control, rather than the System.Web.UI.WebControls.Login control.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top