c# - 如何在没有对象的情况下访问 Page 类的属性(实例)

标签 c# asp.net .net asp.net-mvc

这可能是一个愚蠢的问题,但我有一个困惑。
每个 .aspx 页面都继承了 System.Web.UI.PagePage类具有一些属性,例如 IsPostBackIsValidIsCrossPagePostBack 等等......我们编写 Page.IsPostBack 来访问这些属性IsPostBack
现在,问题是,这些属性是 static 吗,如果不是,那么如何在 .apsx 文件中访问这些属性,我已尝试使用 class 但没有成功。

    public class clsDemo:System.Web.UI.Page
    {
    }  

最佳答案

页面类派生自TemplateControl类;

public class Page : TemplateControl, IHttpHandler

TemplateControl类派生自抽象的Control类;

public abstract class TemplateControl : Control, ...

Page派生的Control类中有一个名为Page的虚属性;

    // Summary:
    //     Gets a reference to the System.Web.UI.Page instance that contains the server
    //     control.
    //
    public virtual Page Page { get; set; }

Page类中有IsPostBackIsValid等属性;

    // Summary:
    //     Gets a value that indicates whether the page is being rendered for the first
    //     time or is being loaded in response to a postback.
    //        
    public bool IsPostBack { get; }

因此,

由于aspx页面派生自Page类,所以它也继承了TemplateControlControl类。在 Control 类中有一个名为 Page 的公共(public)属性,因此您可以访问类中的 Page 属性。并且 Page 类具有公共(public)属性,如 IsPostbackIsValid 等,因此您可以从 Page 属性中使用这些属性.

public class Test : Page
{
    public Test()
    {
        bool test = this.IsCallback;
    }
}

关于c# - 如何在没有对象的情况下访问 Page 类的属性(实例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14659718/

相关文章:

c# - 如何通过将泛型类型传递给基类来触发扩展方法?

c# - 每当创建新类时,向 asp 项目添加命名空间

c# - 调试 winform 崩溃 - C# [ADPlus + Windbg]

c# - 在 JSON 中解析 LINQ 答案

asp.net - DBMS 如何影响应用程序性能?还有 Informix GUI 工具吗?

.net - 如何在 WPF MVVM 中使用用户控件

.net - {"Unable to load DLL ' sqlite3' : The specified module could not be found.(HRESULT 异常:0x8007007E)

c# - 使用 Python 访问 C# 库中的方法 - Interop .dll 文件

c# - 为什么 IsDisposed 在调用 Dispose() 后返回 false?

c# - 错误消息 : "Access to the path c:\windows\microsoft.net\framework\(version)\Temporary ASP.NET Files\(blah) is denied." - what causes this?