工具箱中的ASP.NET visual studio 2013登录控件始终在代码隐藏中显示错误
我正在使用Visual studio 2013,正在开发一个ASP.NET Web应用程序。我从工具箱拖放了登录控件。其表单名称为“Form1”,登录ID为“log1”。我试图访问登录表单中名为“UserName”和“Password”以及“log1.UserName”和“log1.Password”的文本框。我总是遇到这样的错误,'名称'log1'在当前上下文中不存在。我该怎么办?有人能帮忙吗?提前谢谢 我的代码隐藏文件home.aspx.cs:工具箱中的ASP.NET visual studio 2013登录控件始终在代码隐藏中显示错误,asp.net,visual-studio-2013,login-control,Asp.net,Visual Studio 2013,Login Control,我正在使用Visual studio 2013,正在开发一个ASP.NET Web应用程序。我从工具箱拖放了登录控件。其表单名称为“Form1”,登录ID为“log1”。我试图访问登录表单中名为“UserName”和“Password”以及“log1.UserName”和“log1.Password”的文本框。我总是遇到这样的错误,'名称'log1'在当前上下文中不存在。我该怎么办?有人能帮忙吗?提前谢谢 我的代码隐藏文件home.aspx.cs: using System; using Sys
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>
登录
用户名:
*
密码:
*
尝试使用FindControl获取文本框控件:
TextBox txtUserName = (TextBox)log1.FindControl("UserName");
TextBox txtPassword = (TextBox)log1.FindControl("Password");
您应该在页面顶部检查您的标记,而我在您的aspx页面标记中看不到该标记
您应该特别检查
1) Inherits
属性,Inherits属性应与代码隐藏文件中的部分类名匹配,该文件继承自System.Web.UI.Page类
2) codebhind
属性,应与正在编码的代码隐藏文件匹配
例如,如果您的页面名是默认的,@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指令及其所有属性的更多详细信息,我建议您查看
希望这有帮助。我添加了@Page指令。这是正确的。@DevjoshI已经添加了@Page指令。我只是在添加代码时错过了它。您的登录控制问题已解决,无法从代码隐藏文件访问。您是否检查了inherits属性,确保代码隐藏文件中的类名拼写正确,以及@page指令。你能编辑这个问题并把页面指令放在问题中吗?拼写正确,它们是我创建新webform时自动生成的文件。我也手动检查过,我不知道那是什么。但log1唯一的问题是红色下划线和错误:“log1在当前上下文中不存在。仍然log1显示错误。它的log1有错误。不是用户名和密码。甚至ur代码中的log1也显示错误。