这可能是一个愚蠢的问题,但我有一个困惑。
每个 .aspx
页面都继承了 System.Web.UI.Page
和 Page类具有一些属性,例如 IsPostBack
、IsValid
、IsCrossPagePostBack
等等......我们编写 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
类中有IsPostBack
、IsValid
等属性;
// 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
类,所以它也继承了TemplateControl
和Control
类。在 Control
类中有一个名为 Page
的公共(public)属性,因此您可以访问类中的 Page
属性。并且 Page
类具有公共(public)属性,如 IsPostback
和 IsValid
等,因此您可以从 Page
属性中使用这些属性.
public class Test : Page
{
public Test()
{
bool test = this.IsCallback;
}
}
关于c# - 如何在没有对象的情况下访问 Page 类的属性(实例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14659718/