C# ASP.net会话登录
我正在尝试在我的web应用程序中实现会话登录。目前,我收到一条错误消息“当前上下文中不存在名称'txtUserName'。当我检查aspx文件时,文本框ID=“txtUserName”存在 txtserName.Text和txtPassword.Text是带有下划线的错误第一个错误 从您的标记中:C# ASP.net会话登录,c#,asp.net,html,session,login,C#,Asp.net,Html,Session,Login,我正在尝试在我的web应用程序中实现会话登录。目前,我收到一条错误消息“当前上下文中不存在名称'txtUserName'。当我检查aspx文件时,文本框ID=“txtUserName”存在 txtserName.Text和txtPassword.Text是带有下划线的错误第一个错误 从您的标记中: <asp:TextBox ID="txtUserName" <asp:TextBox ID="txtPassword" 其次,登录控件中的控件在代码隐藏中无法直接访问。您必须找到该控件并
<asp:TextBox ID="txtUserName"
<asp:TextBox ID="txtPassword"
其次,登录控件中的控件在代码隐藏中无法直接访问。您必须找到该控件并从中获取文本。它应该像
cmd.Parameters["@USerName"].Value =
((TextBox)LoginUser.FindControl("txtUserName")).Text;
cmd.Parameters["@Password"].Value =
((TextBox)LoginUser.FindControl("txtPassword")).Text;
由于控件位于模板中,因此控件在设计时不知道它们,因为无法保证在运行时加载哪个模板,因此需要使用
FindControl
:
var userNameTextBox = LoginUser.FindControl("txtUserName") as TextBox;
if (userNameTextBox != null)
{
//proceed.
}
这也是一个sulotion->LoginUser.txtUserName我现在看到的问题是cmd.Parameters[“@USerName”].Value=txtUserName.Text。但是你指出的是另一个错误。谢谢你指出:)我已经更新了我的答案,现在一定能帮你解决问题谢谢你的帮助:)
if (UserName.Text == [@"Username"] && Password.Text == [@"Password"])
cmd.Parameters["@USerName"].Value =
((TextBox)LoginUser.FindControl("txtUserName")).Text;
cmd.Parameters["@Password"].Value =
((TextBox)LoginUser.FindControl("txtPassword")).Text;
var userNameTextBox = LoginUser.FindControl("txtUserName") as TextBox;
if (userNameTextBox != null)
{
//proceed.
}