我目前有一个问题与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;
}
}任何帮助都将不胜感激,谢谢!
发布于 2017-03-16 03:49:15
问题是aspx.cs文件中的类名与设计器文件中的类名不同。将代码隐藏文件中的类从LoggedIn重命名为Homepage可以解决此错误。
发布于 2017-03-16 03:50:50
尝尝这个
Label lb = (Label)FindControl("userLabel");
lb.Text = "New text";https://stackoverflow.com/questions/42819251
复制相似问题