现在的位置: 首页 > 综合 > 正文

2004-8-8+ 关于Page这个词

2011年09月18日 ⁄ 综合 ⁄ 共 1677字 ⁄ 字号 评论关闭

public class aspxpage : System.Web.UI.Page
{
   private void Page_Load(object sender, System.EventArgs e)
   {
      Response.Write("hello asp.net!");//a
      this.Response.Write("hello asp.net!");
//b
      Page.Response.Write("hello asp.net!");
//c
      Parent.Page.Response.Write("hello asp.net!");
//d
      this.Page.Response.Write("hello asp.net!");
//e
   }
}

来研究一下上面的这几个东东
0.对Response本身的解释。
Response是一个属性,用于获取与 Page 关联的HttpResponse对象。该对象使您得以将 HTTP 响应数据发送到客户端,并包含有关该响应的信息。而HttpResponse类的方法和属性通过HttpApplication、HttpContext、Page 和 UserControl类的Response属性公开。
a. 直接使用Response。
因为aspxpage是继承自System.Web.UI.Page,而Response是Page类的一个public的属性,所以自然可以直接使用。
b. 用this关键字。
这个也没什么好说的,this嘛~~
c.用页面的Page属性。
这个Page属性从哪里来?首先我们要明白的是,一个.aspx页面实际上也是一个服务器控件,也就是说在加载页面的时候调用了System.Web.UI.Control的一些方法来完成整个页面的生成工作。而在生成时会产生一些属性来方便用户对页面的操作,这个Page属性就是这里来的,它的类型是System.Web.UI.Page,如果还不明白,在下面有详细的解释。
d.用页面的Parent属性。
Parent是获取对页UI层次结构中服务器控件的父控件的引用。这个属性的解释,请参照上一条。
e.用this关键字。
用this来调用本身的属性……

看一下Page和Parent的产生。
页面的生成实际上调用的是System.Web.UI.Control.AddedControl()方法,这是一个私有方法,在控件被添加到另一控件的 Controls 集合后,会立即调用AddedControl()方法。下面是System.Web.UI.Control类的部分代码:

//私有域
private System.Web.UI.Control _parent;
internal System.Web.UI.Page _page;
//AddedControl方法
protected internal virtual void AddedControl(Control control, int index)
{
   if (control._parent != null)
   {
      control._parent.Controls.Remove(control);
   }
   control._parent = this;
   control._page = this._page;
   ...
   ...其他代码
}
//Page属性
public virtual Page Page
{
   get
   {
      if ((this._page == null) && (this._parent != null))
      {
         this._page = this._parent.Page;
      }
      return this._page;
   }
   set
   {
      this._page = value;
   }
}
//Parent属性
public virtual Control Parent
{
   get
   {
      return this._parent;
   }
}

就是这样。
这个问题困扰了我好久,最后还是在csdn得到了解决 -->点这里看原帖<-----
在这里谢谢思归的帮助。

【上篇】
【下篇】

抱歉!评论已关闭.