控件开发学习笔记(1)

1. 选择基类

  • Control类,所有控件都直接或间接继承该类,提供了各类控件通用属性和方法,如唯一标志ID 属性、可见性Visible 等,灵活性最强
  • WebControl类,继承了Control 的所有属性,还增加了布局、可访问性、外观样式等特性;另外,对行为也扩充了好多属性。一般在基于Web 的系统中用得最多,扩展灵活性也很强。
  • CompositeControl类,一般用于将具有一定功能的多个控件集成为一个控件的情况。
  • 继承现有控件,如Label,Button,并在此基础上扩展或改变(通过override 重载其方法实现)其功能,满足业务需要。

2. 控件呈现

在WebControl 基类中,以Render 开头的呈现方法有如下几个:

  •  RenderControl(HtmlTextWriter writer)
    •  Render(HtmlTextWriter writer)
      •  RenderBeginTag(HtmlTextWriter writer)
      •  RenderContents(HtmlTextWriter output)
        •  RenderChildren(HtmlTextWriter writer)
      •  RenderEndTag(HtmlTextWriter writer)

其中在RenderControl 方法内部会调用Render 方法,在Render 方法内部会依次调用RenderBeginTag, RenderContents 和RenderEndTag。

而RenderContents调用RenderChildren方法。

RenderControl 和Render 是Control 基类中的方法。一般在开发基本控件时,我们只需重写RenderContents 方法即可。

 1         /// <summary>1.初始化(对应OnInit方法)</summary>
 2         protected override void OnInit(EventArgs e)
 3         {
 4             base.OnInit(e);
 5         }
 6         /// <summary>2.加载控件状态(对应LoadControlState方法)</summary>
 7         protected override void LoadControlState(object savedState)
 8         {
 9             base.LoadControlState(savedState);
10         }
11         /// <summary>3.加载视图状态(对应LoadViewState方法)</summary>
12         protected override void LoadViewState(object savedState)
13         {
14             base.LoadViewState(savedState);
15         }
16         /// <summary>4.加载回传数据(对应LoadPostData方法)</summary>
17         public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
18         {
19             return false;
20         }
21         /// <summary>5.装载(对应OnLoad方法)</summary>
22         protected override void OnLoad(EventArgs e)
23         {
24             base.OnLoad(e);
25         }
26         /// <summary>6.数据回传事件通知(对应RaisePostDataChangedEvent方法)</summary>
27         public virtual void RaisePostDataChangedEvent()
28         {
29         }
30         /// <summary>7.触发回发事件(对应RaisePostBackEvent方法)</summary>
31         public virtual void RaisePostBackEvent(string eventArgument)
32         {
33         }
34         /// <summary>8.预呈现(对应OnPreRender方法)</summary>
35         protected override void OnPreRender(EventArgs e)
36         {
37             base.OnPreRender(e);
38         }
39         /// <summary>9.保存视图状态(对应SaveViewState方法)</summary>
40         protected override object SaveViewState()
41         {
42             base.ChildControlsCreated = false;
43             return base.SaveViewState();
44         }
45         /// <summary>10.呈现(对应RenderControl方法)</summary>
46         public override void RenderControl(HtmlTextWriter writer)
47         {
48             base.RenderControl(writer);
49         }
50         /// <summary>11.呈现(对应Render方法)</summary>
51         protected override void Render(HtmlTextWriter writer)
52         {
53             base.Render(writer);
54         }
55         /// <summary>12.呈现(对应RenderBeginTag方法)</summary>
56         public override void RenderBeginTag(HtmlTextWriter writer)
57         {
58             base.RenderBeginTag(writer);
59         }
60         /// <summary>13.呈现(对应RenderContents方法)</summary>
61         protected override void RenderContents(HtmlTextWriter writer)
62         {
63             base.RenderContents(writer);
64         }
65         /// <summary>14.呈现(对应RenderChildren方法)</summary>
66         protected override void RenderChildren(HtmlTextWriter writer)
67         {
68             base.RenderChildren(writer);
69         }
70         /// <summary>15.呈现(对应RenderEndTag方法)</summary>
71         public override void RenderEndTag(HtmlTextWriter writer)
72         {
73             base.RenderEndTag(writer);
74         }
75         /// <summary>16.卸载(对应OnUnload方法)</summary>
76         protected override void OnUnload(EventArgs e)
77         {
78             base.OnUnload(e);
79         }
80         /// <summary>17.卸载(对应OnUnload方法)</summary>
81         public override void Dispose()
82         {
83             base.Dispose();
84         }

 

 

posted @ 2013-01-24 10:23  zhuhc  阅读(185)  评论(0编辑  收藏  举报