微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

主属性不是在 aspx 设计器中自动生成导致运行时错误

如何解决主属性不是在 aspx 设计器中自动生成导致运行时错误

我有一个使用 MasterPageFile内容页面,在代码中我们尝试访问主属性 Master.SessionId

<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/AdminFrontend.Master" AutoEventWireup="true"
    CodeBehind="Reasons.aspx.cs" Inherits="Admin.Other.Reasons" %>

<asp:Content ID="Content2" ContentPlaceHolderID="cphMainBody" runat="server">
    <reason session-id="<%=Master.SessionId%>">
    </reason>
</asp:Content>

但是 Master.SessionId 无法识别,Master 不是指正确的主文件。类似的代码适用于项目中的另一个文件。我们发现的唯一显着区别是,正常工作的页面在 aspx.designer.cs 文件中具有以下自动生成代码

public partial class MyChart {
        
        /// <summary>
        /// Master property.
        /// </summary>
        /// <remarks>
        /// Auto-generated property.
        /// </remarks>
        public new Admin.AdminFrontend Master {
            get {
                return ((Admin.AdminFrontend)(base.Master));
            }
        }
    }

这是我 Reasons.aspx 文件的设计器。

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated. 
// </auto-generated>
//------------------------------------------------------------------------------

namespace Admin.Other
{


    public partial class Reasons
    {
    }
}

I am not sure what is the problem,why in one file that property is auto-generated and not in the other. I thought Visual Studio is doing something crazy. I restarted VS2019 and also tried restarting my machine. Both didn't solve the 

问题。

解决方法

“Reasons.aspx.cs”甚至可能没有“设计器”页面。请检查以确保它存在于 Reasons.aspx 页面(打开解决方案资源管理器并查看与 Reason.aspx 文件相关联的文件)。如果 Designer 文件不存在,请在解决方案资源管理器中选择/突出显示 Reason.aspx 文件,然后使用上方菜单栏(在 Visual Studio 中),然后选择 Project > Convert to Web Application强> 选择。如果“项目”下拉菜单中缺少该选项,这通常表示所有设计器文件都已连接到您的 CodeBehind 文件。

如果您有 Reasons.aspx.designer.cs,请在此处提供代码,以便我们将其与您的 MyChart 示例中的代码进行比较。

,

在您的 Reasons.aspx.cs 文件中, 您可以添加公共属性并返回 Master.sessionID。

例如;

public partial class MyChart
{ 
     public string MySession
     { 
       get
       {
        return Master.SessionID;
       }
     }
}

在您的 aspx 文件中,使用

 <reason session-id="<%=MySession%>"></reason>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。