.net 控件生命周期
这里列举出来了11个生命周期,一般的控件生命周期会经历这11个生命周期,但是有一些特别的控件比如页面控件System.Web.UI.Page等。
具体代码参考如下:
/// <summary> /// 1. 初始化 /// </summary> /// <param name="e"></param> protected override void OnInit(EventArgs e) { OutPut("1. OnInit"); base.OnInit(e); this.Page.RegisterRequiresPostBack(this); } /// <summary> /// 2. 装载视图 /// </summary> /// <param name="savedState"></param> protected override void LoadViewState(object savedState) { OutPut("2. LoadViewState"); base.LoadViewState(savedState); } /// <summary> /// 3. 回传数据处理 /// </summary> /// <param name="postDataKey"></param> /// <param name="postCollection"></param> /// <returns></returns> public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) { OutPut("3. LoadPostData"); return true; } /// <summary> /// 4. 装载时间 /// </summary> /// <param name="e"></param> protected override void OnLoad(EventArgs e) { OutPut("4. OnLoad"); base.OnLoad(e); } /// <summary> /// 5. 回传事件通知 /// </summary> public virtual void RaisePostDataChangedEvent() { OutPut("5. RaisePostDataChangedEvent"); } /// <summary> /// 6. 处理回发事件 /// </summary> /// <param name="eventArgument"></param> public virtual void RaisePostBackEvent(string eventArgument) { OutPut("6. RaisePostBackEvent"); } /// <summary> /// 7. 预呈现 /// </summary> /// <param name="e"></param> protected override void OnPreRender(EventArgs e) { OutPut("7. OnPreRender"); base.OnPreRender(e); } /// <summary> /// 8 保存视图状态 /// </summary> /// <returns></returns> protected override object SaveViewState() { OutPut("8. SaveViewState"); base.SaveViewState(); return new Pair(); } /// <summary> /// 9. 呈现 /// </summary> /// <param name="writer"></param> protected override void Render(HtmlTextWriter writer) { writer.Write("<input type=‘button‘ name=\"{0}\" value=\"Click Me!\" style=‘position: absolute; left: 20px; top: 280px;‘ onclick=\"{1}\" />", "[控件声明周期]", Page.ClientScript.GetPostBackEventReference(this, "")); OutPut("9. Render"); base.Render(writer); } /// <summary> /// 10. 卸载 /// </summary> /// <param name="e"></param> protected override void OnUnload(EventArgs e) { OutPut("10. OnUnload"); base.OnUnload(e); } /// <summary> /// 11. 释放资源 /// </summary> public override void Dispose() { OutPut("11. Dispose"); base.Dispose(); } private void OutPut(string strText) { if (this.DesignMode == false) { HttpContext.Current.Response.Write(strText + "<br />"); } } [Bindable(true)] [Category("Appearance")] [DefaultValue("")] [Localizable(true)] public string Text { get { String s = (String)ViewState["Text"]; return ((s == null) ? String.Empty : s); } set { ViewState["Text"] = value; } } protected override void RenderContents(HtmlTextWriter output) { output.Write(Text); }
这个代码复制到asp.net 服务器控件中,即可调试。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。