首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASP在当前上下文中不存在错误

ASP在当前上下文中不存在错误
EN

Stack Overflow用户
提问于 2017-03-16 03:42:10
回答 2查看 370关注 0票数 0

我目前有一个问题与ASP说,我的代码中的一个项目不存在,但它存在于设计器文件中,并被正确命名的ASPX和ASPX.CS文件,我不知道为什么会发生这个问题。因为它说userLabel目前不存在,我似乎不能理解为什么我的其他页面工作正常。

ASPX代码:

代码语言:javascript
复制
<%@ 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代码:

代码语言:javascript
复制
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);
}
}

设计器文件:

代码语言:javascript
复制
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;
}
}

任何帮助都将不胜感激,谢谢!

EN

回答 2

Stack Overflow用户

发布于 2017-03-16 03:49:15

问题是aspx.cs文件中的类名与设计器文件中的类名不同。将代码隐藏文件中的类从LoggedIn重命名为Homepage可以解决此错误。

票数 3
EN

Stack Overflow用户

发布于 2017-03-16 03:50:50

尝尝这个

代码语言:javascript
复制
Label lb = (Label)FindControl("userLabel");
lb.Text = "New text";
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42819251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档