从Web应用程序项目中的ASPX页面访问代码范围的属性



我在Web应用程序项目中的代码范围文件中有一个属性 x 。它给我编译时间错误,而网站项目中没有任何错误

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" 
Inherits="test.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
<div>
<%= x2 %>
<%# x2 %>
</div>
</form>
</body>
</html>

CodeBehind文件:

using System;
namespace test
{public partial class WebForm1 : System.Web.UI.Page
{
    public int x2 = 2;
    protected void Page_Load(object sender, EventArgs e)
    {          
    }     
   }
}

当我在网站项目中使用相同技术时,它运行良好请帮助谢谢

检查您的代码中是否还有其他错误 webform1.aspx.cs。例如缺少参考或任何其他编译错误。

这对我有用。您确定ViewState中有一个值吗?在表单上载荷为测试增加了一个值。

ViewState["FormMode"] = "ViewApplicant";
lblMessage.DataBind();

还要确保标签有一些文本可以查看。它可能显示但是空的。

<asp:Label ID="lblMessage" runat="server" 
    Text="some text" 
    Visible='<%# FormMode == "View" || FormMode == "ViewApplicant" %>'>
</asp:Label>

您的代码完全可以。在访问变量/属性Initillay时,红色下划线可能会出现在表示编译错误的表达式下,但是当您构建解决方案时,应该没有错误。

您的原始帖子已更改,因此这是一个新答案。

尝试从后面的代码中删除名称空间,然后将页面上的"继承"更改为 Inherits="WebForm1"

页面指令:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
    Inherits="WebForm1" %>

代码背后:

using System;
public partial class WebForm1 : System.Web.UI.Page
{
    public int x2 = 2;
    protected void Page_Load(object sender, EventArgs e)
    {
    }     
}

'继承'必须与部分类名称匹配。

最新更新