ASP.net名称' userName'在当前上下文中不存在

时间:2014-09-04 12:52:30

标签: c# asp.net

我点击了20多个链接,尝试在不同的网站上解决此问题。

请帮帮我

所以我正在尝试进行简单的身份验证登录。

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

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

}
protected void loginButton_Click(object sender, EventArgs e)
{
    if(FormsAuthentication.Authenticate(UserName.Text, Password.Text)
    {

    }
}
}

但会弹出一条错误消息,提示"名称' userName'在当前背景下不存在'和名称'密码'在当前背景下不存在。

这是aspx代码

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"      CodeFile="Login.aspx.cs" Inherits="Account_Login" %>

 <asp:Content ID="Content3" ContentPlaceHolderID="MainContent" Runat="Server">
<hgroup class="title">
    <h1>Login Page</h1>
</hgroup>
<section id="loginForm">
    <asp:Login ID="Login1" runat="server" ViewStateMode="Disabled" RenderOuterTable="false">
        <LayoutTemplate>
            <p class="validation-summary-errors">
                <asp:Literal runat="server" ID="FailureText" />
            </p>
            <fieldset>
                <legend>Log in Form</legend>
                <ol>
                    <li>
                        <asp:Label ID="Label1" runat="server" AssociatedControlID="UserName">User name</asp:Label>
                        <asp:TextBox runat="server" ID="UserName" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="UserName" CssClass="field-validation-error" ErrorMessage="The user name field is required." />
                    </li>
                    <li>
                        <asp:Label ID="Label2" runat="server" AssociatedControlID="Password">Password</asp:Label>
                        <asp:TextBox runat="server" ID="Password" TextMode="Password" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="Password" CssClass="field-validation-error" ErrorMessage="The password field is required." />
                    </li>
                    <li>
                        <asp:CheckBox runat="server" ID="RememberMe" />
                        <asp:Label ID="Label3" runat="server" AssociatedControlID="RememberMe" CssClass="checkbox">Remember me?</asp:Label>
                    </li>
                </ol>
                <asp:Button ID="loginButton" runat="server" CommandName="Login" Text="Log in" OnClick="loginButton_Click" />
            </fieldset>
        </LayoutTemplate>
    </asp:Login>
    <p>
        <a href="Register.aspx">Register</a>
        if you don't have an account.
    </p>
</section>

请帮帮我:)。

3 个答案:

答案 0 :(得分:2)

C#区分大小写。 根据您的控制ID,它应该是带有大写“U”的用户名:

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

你的代码应该是:

protected void loginButton_Click(object sender, EventArgs e)
{
    if(FormsAuthentication.RedirectFromLoginPage(UserName.Text, Password.Text)
    {

    }
}

编辑 - 注意到有问题的控件位于用户控件内,因此代码现在应为:

protected void loginButton_Click(object sender, EventArgs e)
{
    if(FormsAuthentication.RedirectFromLoginPage(Login1.UserName.Text, Password.Text)
    {

    }
}

但是你仍然在UserName ID

中输入了拼写错误

答案 1 :(得分:2)

用户名位于登录控件内,因此您需要通过 Login1 控件进行访问。

string userName = Login1.UserName;
string password = Login1.Password;    

代码中的另一个问题是,如果您使用登录控件,则不应使用 loginButton_Click 事件。

相反,您需要根据您的饱食感使用以下内容 -

  • LoggingIn
  • 的loggedIn

答案 2 :(得分:0)

代码区分大小写。

if(FormsAuthentication.RedirectFromLoginPage(UserName.Text, Password.Text)
{

}