2014-09-04 134 views
0

我点击了20个试图在不同网站上解决这个问题的链接。ASP.net名称'userName'在当前上下文中不存在

请帮我

所以我想做一个简单的登录身份验证。

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) 
    { 

    } 
} 
} 

但错误消息弹出说“这个名字‘用户名’不会在目前情况下存在”和名字‘密码’不在当前情况下存在。

这里是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> 

请帮我:)

+4

该ID用户名不是用户名... – 2014-09-04 12:54:54

+0

哎呀,对不起,我是测试如果骆驼套管影响它。但它并没有忘记改变价值。 但我仍然得到相同的错误,谢谢:) – helloGoodDay 2014-09-04 22:23:00

回答

2

用户名里面登录控制,所以你需要通过Login1控制访问它。

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

在你的代码的另一个问题是,如果你使用登录的控制,你不应该使用loginButton_Click事件。

,不要成为囚,你需要使用下面的根据您的饱食 -

  • LoggingIn
  • 的loggedIn
+0

http:// stackoverflow.com/a/25654827/296861 – Win 2014-09-04 14:10:43

+0

嘿赢:)我已经尝试过这种方式 字符串username = Login1.UserName; 字符串密码= Login1.Password; 如果(FormsAuthentication.RedirectFromLoginPage(用户名,密码)) { } 但是我收到错误信息 – helloGoodDay 2014-09-04 22:29:53

+0

'if(FormsAuthentication.RedirectFro mLoginPage(userName,password)){'不是有效的语句。 ** RedirectFromLoginPage的返回类型**是** void **,因此它不返回任何内容。请参阅我的评论中的[此链接](http://stackoverflow.com/a/25654827/296861)。 – Win 2014-09-04 22:32:47

2

C#是区分敏感。 它应该是一个大写的用户名“U”按照你的控制ID:

<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) 
    { 

    } 
} 

但是你还是确实有在用户名ID

0123错字
+0

**用户名**文本框是**登录**控制。你不能像这样访问它 - ** UserName.Text **。 http://stackoverflow.com/a/25667546/296861 – Win 2014-09-04 14:14:26

+0

你是对的,我错过了那部分。 – 2014-09-04 17:38:10

+0

@Ahmedilyas是的,我忘了将用户名更改为用户名,因为我测试如果骆驼套管影响它。但事实证明它没有。 (Login1.UserName.Text,Password。文本)<如果我在参数中这样做,我得到文本中的用户名字段'字符串'的错误不包含文本'的定义和没有扩展方法'文本'接受类型'字符串'的第一个参数可以被发现(你是否缺少使用指令或程序集引用?) 感谢您的努力:) – helloGoodDay 2014-09-04 22:27:48

0

代码区分大小写。

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

}