2014-09-04 85 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) 
{ 

} 
相關問題