我目前在ASP中遇到问题,说我的代码中的一个项目不存在,但它存在于设计器文件中,并且在ASPX和ASPX.CS文件上都正确命名,我无法弄清楚为什么这个问题是发生的历史。因为它说userLabel目前不存在,我似乎无法理解为什么我的其他页面正常工作。
ASPX代码:
<%@ Page Language="C#" AutoEventWireup="True" CodeBehind="Homepage.aspx.cs" Inherits="Assignment2ASP.Homepage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Logged In Page</title>
</head>
<body>
<p>This is the Logged In page</p>
<form id="logoutform" runat="server">
<asp:Button ID="logoutButton" Text="Logout" runat="server" OnClick="logoutEventMethod" />
<p>Hello</p>
<p>
<asp:Label ID="userLabel" Text ="No User" runat="server" />
</p>
</form>
</body>
</html>
ASPX.CS代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class LoggedIn : System.Web.UI.Page
{
String name;
protected void Page_Load(object sender, EventArgs e)
{
name = (String)(Session["uname"]);
if (name == null)
{
Response.BufferOutput = true;
Response.Redirect("index.aspx", true);
}
else
{
userLabel.Text = name;
}
}
protected void logoutEventMethod(object sender, EventArgs e)
{
Session["uname"] = null;
Session.Abandon();
Response.BufferOutput = true;
Response.Redirect("index.aspx", true);
}
}
设计师档案:
namespace Assignment2ASP {
public partial class Homepage {
/// <summary>
/// logoutform control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlForm logoutform;
/// <summary>
/// logoutButton control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Button logoutButton;
/// <summary>
/// userLabel control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label userLabel;
}
}
任何帮助都会受到极大的赞赏谢谢!
答案 0 :(得分:2)
问题是aspx.cs文件中的类名与设计器文件中的类名不同。将代码隐藏文件中的类从LoggedIn重命名为Homepage可以解决错误。
答案 1 :(得分:-1)
试试这个
Label lb = (Label)FindControl("userLabel");
lb.Text = "New text";