ASP.net“用户名”的名称并没有在目前的情况下存在 [英] ASP.net The name 'userName' does not exist in the current context

查看:124
本文介绍了ASP.net“用户名”的名称并没有在目前的情况下存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我点击了20个链接在尝试不同的网站来解决这个问题。

I've clicked over 20 links trying to solve this problem in different websites.

请帮我

所以我试图做一个简单的日志中的身份验证。

So I am trying to do a simple log in authentication.

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

    }
}
}

但是,错误消息弹出说用户名不会在目前情况下中的姓名与密码存在名称'并不在目前的情况下存在。

But an error message pops up saying "The name 'userName' does not exist in the current context' and The name 'Password' does not exist in the current context.

下面是ASPX code

Here is the aspx code

<%@ 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>

请帮我:)

推荐答案

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

UserName is inside Login control, so you need to access it via Login1 control.

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

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

Another problem in your code is, you should not use loginButton_Click event if you use Login control.

在代替,你需要使用下面的根据您的饱食 -

In stead, you need to use the following depending on your satiation -


  • LoggingIn

  • 的loggedIn

这篇关于ASP.net“用户名”的名称并没有在目前的情况下存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆