|
首冼回顾一下 3cjLinux联盟 Page页的生命周期 3cjLinux联盟 3cjLinux联盟 PreInit 3cjLinux联盟 Init 3cjLinux联盟 InitComplete 3cjLinux联盟 LoadState 3cjLinux联盟 ProcessPostData 3cjLinux联盟 PreLoad 3cjLinux联盟 Load 3cjLinux联盟 LoadComplete 3cjLinux联盟 PreRender 3cjLinux联盟 PrepareCallback //如果有回调 3cjLinux联盟 PreRenderComplete 3cjLinux联盟 SaveState 3cjLinux联盟 SaveStateComplete 3cjLinux联盟 Render 3cjLinux联盟 3cjLinux联盟 不论是Asp.net请求,还是Ajax请求,都要执行上面的页生命周期, 3cjLinux联盟 3cjLinux联盟 在Page页最后会调用Page.RenderControl()呈现Page所有子控件 3cjLinux联盟 3cjLinux联盟 流程如下 3cjLinux联盟 3cjLinux联盟 Page.RenderControl 3cjLinux联盟 Control.RenderControl() 3cjLinux联盟 Control.RenderControl(writer,this.Adapter) 3cjLinux联盟 Control.RenderControlInternal() 3cjLinux联盟 Control.Render() //此时Page.Render()将其重写 3cjLinux联盟 Control.RenderChildren() 3cjLinux联盟 Control.RenderControlInternal() 3cjLinux联盟 3cjLinux联盟 现在关键所在 3cjLinux联盟 Control.RenderControlInternal()实现如下 3cjLinux联盟 如果不是Asp.net Ajax处理,肯定会执行foreach依次对所有的子控件进行遍历 3cjLinux联盟 3cjLinux联盟 internal void RenderChildrenInternal(HtmlTextWriter writer, ICollection children) 3cjLinux联盟 { 3cjLinux联盟 //正常情况如果不设置RareFields,进行默认的流程依次对所有子控件进行处理, 3cjLinux联盟 //如果设置RareFields,意思就是以自定义的方式改写RenderChildren 3cjLinux联盟 if ((this.RareFields != null) && (this.RareFields.RenderMethod != null)) //当您SetRenderMethodDelegate时,RareFieldsEnsured就有值啦 3cjLinux联盟 { 3cjLinux联盟 writer.BeginRender(); 3cjLinux联盟 3cjLinux联盟 //调用RenderMethod委托,Asp.net Ajax1.0在使用Control.SetRenderMethodDelegate时,对应的回调函数就是在此时处理的 3cjLinux联盟 this.RareFields.RenderMethod(writer, this); 3cjLinux联盟 writer.EndRender(); 3cjLinux联盟 } 3cjLinux联盟 else if (children != null) 3cjLinux联盟 { 3cjLinux联盟 //如果不设置这个RareFieldsEnsured,会把Page里所有的控件呈现 3cjLinux联盟 //依次调用Control里所有子控件的RenderControl 3cjLinux联盟 foreach (Control control in children) 3cjLinux联盟 { 3cjLinux联盟 control.RenderControl(writer); 3cjLinux联盟 } 3cjLinux联盟 } 3cjLinux联盟 3cjLinux联盟 } 3cjLinux联盟 3cjLinux联盟 以上是Asp.net 的Render处理 3cjLinux联盟 肯定有人要问上面的if是干吗的,这个可是微软流的接口呀,整个asp.net ajax全靠这个接口进行处理 3cjLinux联盟 3cjLinux联盟 在Asp.netAjax1.0上,你肯定要放一个ScriptManager吧 3cjLinux联盟 它也是Control的子类, 3cjLinux联盟 在Page生命周期的OnPreRender时,它会调用 3cjLinux联盟 3cjLinux联盟 PageRequestManager.OnPreRender(); 3cjLinux联盟 3cjLinux联盟 3cjLinux联盟 问题就在这,PageRequestManager.OnPreRender()实现如下 3cjLinux联盟 3cjLinux联盟 _owner.IPage.SetRenderMethodDelegate(RenderPageCallback); 3cjLinux联盟 3cjLinux联盟 SetRenderMethodDelegate()方法是干什么用的 3cjLinux联盟 3cjLinux联盟 在Control中,当您调用了SetRenderMethodDelegate会给Control.RareFieldsEnsured设置值 3cjLinux联盟 public void SetRenderMethodDelegate(RenderMethod renderMethod) 3cjLinux联盟 { 3cjLinux联盟 //使用OccasionalFields.RareField 3cjLinux联盟 this.RareFieldsEnsured.RenderMethod = renderMethod; 3cjLinux联盟 this.Controls.SetCollectionReadOnly("Collection_readonly_Codeblocks"); 3cjLinux联盟 } 3cjLinux联盟 3cjLinux联盟 到这一切就清晰了,当您设置了RenderPageCallback,正常的Asp.net流程就不会再走了,所有的子控件就不会被Render 3cjLinux联盟 在Asp.netAjax 1.0中使用 3cjLinux联盟 3cjLinux联盟 private void RenderPageCallback(HtmlTextWriter writer, Control pageControl) 3cjLinux联盟 { 3cjLinux联盟 ... 3cjLinux联盟 //取出当前的HtmlForm 3cjLinux联盟 IHtmlForm formControl = _owner.IPage.Form; 3cjLinux联盟 3cjLinux联盟 //手动设置当FormControl.RenderControl时()回调方法 ,因为这个时候Page.Controls里有子控件不会被Render啦,怎么办 3cjLinux联盟 //手动处理一个呗 3cjLinux联盟 formControl.SetRenderMethodDelegate(RenderFormCallback); 3cjLinux联盟 3cjLinux联盟 //同样,HtmlForm.RenderControl不会在Page.Controls里自动触发的 3cjLinux联盟 //这个时候您需要手动的RenderCtronl一下,这样才能调用RenderFormCallback 3cjLinux联盟 formControl.RenderControl(formWriter); 3cjLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|