2011-08-20 144 views
2

我想實現一個會話登錄到我的web應用程序。目前,我收到一條錯誤消息「‘txtUserName’這個名字在目前情況下不存在。當我檢查ASPX文件的文本ID =」 txtUserName」的存在。ASP.net會話登錄

<h2> 
    Log In 
</h2> 
<p> 
    Please enter your username and password. 
    <asp:HyperLink ID="RegisterHyperLink" runat="server" EnableViewState="false">Register</asp:HyperLink> if you don't have an account. 
</p> 
<asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false"> 
    <LayoutTemplate> 
     <span class="failureNotification"> 
      <asp:Literal ID="FailureText" runat="server"></asp:Literal> 
     </span> 
     <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification" 
      ValidationGroup="LoginUserValidationGroup"/> 
     <div class="accountInfo"> 
      <fieldset class="login"> 
       <legend>Account Information</legend> 
       <p> 
        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label> 
        <asp:TextBox ID="txtUserName" runat="server" CssClass="textEntry"></asp:TextBox> 
        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
         CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." 
         ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator> 
       </p> 
       <p> 
        <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label> 
        <asp:TextBox ID="txtPassword" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox> 
        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" 
         CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." 
         ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator> 
       </p> 
       <p> 
        <asp:CheckBox ID="RememberMe" runat="server"/> 
        <asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">Keep me logged in</asp:Label> 
       </p> 
      </fieldset> 
      <p class="submitButton"> 
       <asp:Button ID="LoginButton" Onclick="LoginButton_Click" runat="server" CommandName="Login" Text="Log In" ValidationGroup="LoginUserValidationGroup"/> 
      </p> 
     </div> 
    </LayoutTemplate> 
</asp:Login> 

我當前的代碼背後文件是:

using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Data.SqlClient; 
using System.Web.Configuration; 
using System.Collections.Generic; 

namespace Project.Account 
{ 
    public partial class Login : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]); 
     } 

     protected void LoginButton_Click(object sender, EventArgs e) 
     { 

      string connectionString = WebConfigurationManager.ConnectionStrings["DBConnectionString1"].ConnectionString; 


      SqlConnection con = new SqlConnection(connectionString); 
      SqlCommand cmd = new SqlCommand("LoginUsers", con); 
      cmd.CommandType = CommandType.StoredProcedure; 


      cmd.Parameters.Add(new SqlParameter("@UserName", SqlDbType.VarChar, 20)); 
      cmd.Parameters["@UserName"].Value = txtUserName.Text; 

      cmd.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar, 20)); 
      cmd.Parameters["@Password"].Value = txtPassword.Text; 


         } 
    } 
} 

txtUSerName.Text和txtPassword.Text是有下劃線的錯誤

回答

1

的第一個錯誤

從你的標記之一:

<asp:TextBox ID="txtUserName" 
<asp:TextBox ID="txtPassword" 

但在後面的代碼,您使用的是:

if (UserName.Text == [@"Username"] && Password.Text == [@"Password"]) 

其次,是在Login控件控件是不是在後面的代碼直接訪問。你必須找到控制並從中獲取文本。它應該是這樣......

cmd.Parameters["@USerName"].Value = 
        ((TextBox)LoginUser.FindControl("txtUserName")).Text; 

cmd.Parameters["@Password"].Value = 
        ((TextBox)LoginUser.FindControl("txtPassword")).Text; 
+0

目前我看這個問題是在cmd.Parameters [「@用戶名」]值。 = txtUserName.Text。但是你指出的是另一個錯誤。感謝您指出:) – nick

+0

我有更新我的答案,它現在必須幫助解決您的問題 –

+0

尤里卡!有用!謝謝你:P – nick

3

由於控件在模板中,控制不是在設計時意識到它們的存在,因爲它不能保證哪個模板,因爲這是在運行時完成加載,所以你需要使用FindControl

var userNameTextBox = LoginUser.FindControl("txtUserName") as TextBox; 

if (userNameTextBox != null) 
{ 
    //proceed. 
} 
+0

謝謝我感謝幫助:) – nick

+0

....隨時。 :) –

1

這也是sulotion - > LoginUser.txtUserName