2015-07-13 48 views
0

我正在使用Visual Studio 2013,並且正在開發ASP.NET Web應用程序。我從工具箱拖放登錄控件。它的表單名稱是'Form1',登錄ID是'log1'。我嘗試訪問名爲「UserName」的登錄表單中的文本框和「log1.UserName」和「log1.Password」中的「Password」。我總是得到這個錯誤,'名稱'log1'在當前上下文中不存在。我該怎麼辦?誰能幫忙?提前致謝!工具箱中的ASP.NET visual studio 2013登錄控件始終在代碼後面顯示錯誤

我的文件home.aspx.cs背後代碼:

using System; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
using System.Data.SqlClient; 
public partial class Home : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 
     } 
    } 
    static int count = 0; 
protected void log1_Authenticate(object sender, AuthenticateEventArgs e) 
{ 
    if (log1.UserName == "Admin" && log1.Password == "Admin") 
    { 
     Response.Redirect("Adminhome.aspx"); 
    } 
    else if (YourValidationFunction(log1.UserName, log1.Password)) 
    { 
     Session["User"]=log1.UserName; 
     e.Authenticated = true; 
     Response.Redirect("userhome.aspx"); 
     log1.TitleText = "Successfully Logged In"; 
    } 
    else 
    { 
     e.Authenticated = false; 
     count++; 
     if (count >= 3) 
     { 
      count = 0; 
      Session["User"] = log1.UserName; 
      Server.Transfer("MainPage.aspx"); 
     } 
    } 
} 
SqlConnection strConnection = new SqlConnection("server=.\\SQLEXPRESS;database=honeypot;integrated  security=true;"); 
    private bool YourValidationFunction(string UserName, string Password) 
    { 
     bool boolReturnValue = false; 
     String SQLQuery = "SELECT UserName, Password FROM Register"; 
     SqlCommand command = new SqlCommand(SQLQuery, strConnection); 
     SqlDataReader Dr; 
     strConnection.Open(); 
     Dr = command.ExecuteReader(); 
     while (Dr.Read()) 
     { 
      if ((UserName == Dr["UserName"].ToString()) & (Password == Dr["Password"].ToString())) 
      { 
       boolReturnValue = true; 
      } 
     } 
     Dr.Close(); 
     return boolReturnValue; 
    } 
    protected void lnkRegis_Click(object sender, EventArgs e) 
    { 
     Response.Redirect("AdUserAcc.aspx"); 
    } 
} 

Home.aspx:

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 

    </div> 
     <asp:Login ID="log1" runat="server"> 
      <LayoutTemplate> 
       <table cellpadding="1" cellspacing="0" style="border-collapse:collapse;"> 
        <tr> 
         <td> 
          <table cellpadding="0"> 
           <tr> 
            <td align="center" colspan="2">Log In</td> 
           </tr> 
           <tr> 
            <td align="right"> 
             <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label> 
            </td> 
            <td> 
             <asp:TextBox ID="UserName" runat="server"></asp:TextBox> 
             <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="log1">*</asp:RequiredFieldValidator> 
            </td> 
           </tr> 
           <tr> 
            <td align="right"> 
             <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label> 
            </td> 
            <td> 
             <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox> 
             <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="log1">*</asp:RequiredFieldValidator> 
            </td> 
           </tr> 
           <tr> 
            <td colspan="2"> 
             <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." /> 
            </td> 
           </tr> 
           <tr> 
            <td align="center" colspan="2" style="color:Red;"> 
             <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal> 
            </td> 
           </tr> 
           <tr> 
            <td align="right" colspan="2"> 
             <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="log1" /> 
            </td> 
           </tr> 
          </table> 
         </td> 
        </tr> 
       </table> 
      </LayoutTemplate> 
     </asp:Login> 

回答

0

嘗試使用的FindControl來獲取TextBox控件:

TextBox txtUserName = (TextBox)log1.FindControl("UserName"); 
TextBox txtPassword = (TextBox)log1.FindControl("Password"); 
+0

仍然log1顯示錯誤。它的log1有錯誤。不是用戶名和密碼。即使在我們的代碼中的log1也顯示錯誤。 –

0

你應該檢查你的@Page directive的頁面頂部哪個我無法看到你的標記的aspx頁面。

尤其是應檢查是否有

1)Inherits屬性,Inherits屬性應該匹配到部分類名在代碼隱藏文件,從System.Web.UI.Page類繼承。

2)CodeBehind屬性,應該匹配到代碼隱藏文件,您將在編碼。

例如,如果你的頁面的名稱是默認情況下,@ Page指令看起來應該像它下面的最低

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
Inherits="WebApplication.Default" %> 

在你的情況下,上述應該是

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" 
Inherits="YourWebApplicationName.Home" %> 

有關@ Page指令的詳細信息和它的所有屬性我禾建議你看到這個MSDN reference

希望這會有所幫助。

+0

我已添加@Page指令。這是正確的。@ Devjosh –

+0

我已經添加了@頁指令。在這裏添加代碼時,我錯過了它 –

+0

是您的問題解決了關於登錄控制無法從代碼隱藏文件訪問的問題,您是否檢查了inherits屬性,確保類名正確地拼寫在代碼隱藏文件中以及您的@頁指令。你可以編輯問題並將頁面指令置於有問題的位置。 – Devjosh

相關問題