首先回顾一下
Page页的生命周期
PreInit
Init
InitComplete
LoadState
ProcessPostData
PreLoad
Load
LoadComplete
PreRender
PrepareCallback //如果有回调
PreRenderComplete
SaveState
SaveStateComplete
Render
不论是Asp.net请求,还是Ajax请求,都要执行上面的页生命周期,在Page页最后会调用Page.RenderControl()呈现Page所有子控件流程如下
Page.RenderControl
Control.RenderControl()
Control.RenderControl(writer,this.Adapter)
Control.RenderControlInternal()
Control.Render() //此时Page.Render()将其重写
Control.RenderChildren()
Control.RenderControlInternal()
现在关键所在
Control.RenderControlInternal()实现如下
如果不是Asp.net Ajax处理,肯定会执行foreach依次对所有的子控件进行遍历
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
internal void RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//正常情况如果不设置RareFields,进行默认的流程依次对所有子控件进行处理,
//如果设置RareFields,意思就是以自定义的方式改写RenderChildren
if ((this.RareFields != null) && (this.RareFields.RenderMethod != null))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
writer.BeginRender();
//调用RenderMethod委托,Asp.net Ajax1.0在使用Control.SetRenderMethodDelegate时,对应的回调函数就是在此时处理的
this.RareFields.RenderMethod(writer, this);
writer.EndRender();
}
else if (children != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//如果不设置这个RareFieldsEnsured,会把Page里所有的控件呈现
//依次调用Control里所有子控件的RenderControl
foreach (Control control in children)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
control.RenderControl(writer);
}
}
}
以上是Asp.net 的Render处理
肯定有人要问上面的if是干吗的,这个可是微软流的接口呀,整个asp.net ajax全靠这个接口进行处理
在Asp.netAjax1.0上,你肯定要放一个ScriptManager吧
它也是Control的子类,
在Page生命周期的OnPreRender时,它会调用
PageRequestManager.OnPreRender();
问题就在这,PageRequestManager.OnPreRender()实现如下
_owner.IPage.SetRenderMethodDelegate(RenderPageCallback);
SetRenderMethodDelegate()方法是干什么用的
在Control中,当您调用了SetRenderMethodDelegate会给Control.RareFieldsEnsured设置值
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
public void SetRenderMethodDelegate(RenderMethod renderMethod)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//使用OccasionalFields.RareField
this.RareFieldsEnsured.RenderMethod = renderMethod;
this.Controls.SetCollectionReadOnly("Collection_readonly_Codeblocks");
}
![](/Images/OutliningIndicators/None.gif)
到这一切就清晰了,当您设置了RenderPageCallback,正常的Asp.net流程就不会再走了,所有的子控件就不会被Render
在Asp.netAjax 1.0中使用
![](/Images/OutliningIndicators/ContractedBlock.gif)
Code
private void RenderPageCallback(HtmlTextWriter writer, Control pageControl)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
//取出当前的HtmlForm
IHtmlForm formControl = _owner.IPage.Form;
//手动设置当FormControl.RenderControl时()回调方法 ,因为这个时候Page.Controls里有子控件不会被Render啦,怎么办
//手动处理一个呗
formControl.SetRenderMethodDelegate(RenderFormCallback);
//同样,HtmlForm.RenderControl不会在Page.Controls里自动触发的
//这个时候您需要手动的RenderCtronl一下,这样才能调用RenderFormCallback
formControl.RenderControl(formWriter);
//生成客户端回调信息|asyncPostBackControlIDs
EncodeString(writer, AsyncPostBackControlIDsToken, String.Empty, GetAsyncPostBackControlIDs(false));
//生成客户端回调信息 |postBackControlIDs
EncodeString(writer, PostBackControlIDsToken, String.Empty, GetPostBackControlIDs(false));
//生成客户端回调信息 |updatePanelIDs|tUpdatePanel
EncodeString(writer, UpdatePanelIDsToken, String.Empty, GetAllUpdatePanelIDs());
//生成客户端回调信息 |childUpdatePanelIDs
EncodeString(writer, ChildUpdatePanelIDsToken, String.Empty, GetChildUpdatePanelIDs());
//生成客户端回调信息 |panelsToRefreshIDs
EncodeString(writer, UpdatePanelsToRefreshToken, String.Empty, GetRefreshingUpdatePanelIDs());
//生成客户端回调信息 |asyncPostBackTimeout
EncodeString(writer, AsyncPostBackTimeoutToken, String.Empty, _owner.AsyncPostBackTimeout.ToString(CultureInfo.InvariantCulture));
}
private void RenderFormCallback(HtmlTextWriter writer, Control containerControl)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
//呈现所有需要回发的的UpdatePanel
if (_updatePanelsToRefresh != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach (UpdatePanel panel in _updatePanelsToRefresh)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (panel.Visible)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
panel.RenderControl(_updatePanelWriter);
// 会生成如何的客户端回调信息
//169|updatePanel|UpdatePanel1|\r\n
<input type=\"submit\" name=\"Button1\" value=\"Button\" id=\"Button1\" />\r\n
<input name=\"txt\" type=\"text\" value=\"wxy\" id=\"txt\" />\r\n
}
}
}
//依次将页面上所有控件的RenderControl,写入dummyWriter
foreach (Control control in containerControl.Controls)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//将整个Page页上所有的控件写入垃圾器,
//此时,这些控件Render的信息不会发送到客户端
control.RenderControl(dummyWriter);
}
![](/Images/OutliningIndicators/InBlock.gif)
注意,在RenderFormCallback时,HttpRequest会手动的Flush()将上面的信息刷加IIS
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
至于Ctronl.Render时,是如何写入HtmlTextWriter,再写入HttpWriter,
再写回HttpWorkRequest,最终刷回内核,或使用socket发送回服务器,不在本文讨论范围.......
由上可见,Asp.net Ajax,必没有使用传统的HttpRequest.Filter来处理回发信息,而是使用了Asp.net 2.0内置的方式和回调的
Asp.net Ajax回调后呈现到客户端有二个部分,一部分是由RenderFormCallback生成的如下
RenderFormCallback部分会生成如下的客户端脚本
169|updatePanel|UpdatePanel1|\r\n
<input type=\"submit\" name=\"Button1\" value=\"Button\" id=\"Button1\" />\r\n
<input name=\"txt\" type=\"text\" value=\"wxy\" id=\"txt\" />\r\n
一部分是RenderPageCallback生成的
52|hiddenField
|__VIEWSTATE|/wEPDwUJNjg1NjA3NDcxZGRCX32AH2I/NEq+gCFrVr49kQDECw==|56
|hiddenField|__EVENTVALIDATION|/wEWAwKf344xAoznisYGApKGsMIJOfjKisKPYP0O2NXUpNjnA29hMlA=|0
|asyncPostBackControlIDs|||0|postBackControlIDs|||13|updatePanelIDs||tUpdatePanel1|0
|childUpdatePanelIDs|||12|panelsToRefreshIDs||UpdatePanel1|2|asyncPostBackTimeout||90|12
|formAction||Default.aspx|13|pageTitle||Untitled Page|
在客户端的部分Sys.WebForm._onFormSubmitCompleted()会对所有的信息以Json type:type,id:id,content:content进行封装
取出UpdatePanel,设置其innerhtml,客户端详细部分见赵老大的blogs