两个基本概念:
1、什么是回传?
回传就是HttpPost请求,或者在Url中带有数据的HttpGet请求,通过网页源代码可以看到:
method="post" action="页面名",这说明了即使是Get也会被转到Post。
2、IPostBackEventHandle接口的作用是什么?
接口的实质就是把客户端事件映射到服务器端事件,有两类映射方式:
1、通过UniqueID。
2、通过脚本。
通过两个例子分析两种映射方式:
一、通过UniqueID
SimpleButton,模拟Button实现事件回传,以UniqueID方式回传到服务器端
[
// The DefaultEventAttribute allows a page
// developer to attach a handler to the default
// event by double-clicking on the control.
DefaultEvent("Click"),
DefaultProperty("Text")
] 
public class SimpleButton: WebControl, IPostBackEventHandler
{
[
Bindable(true),
Category("Behavior"),
DefaultValue(""),
Description("The text to display on the button")
]
public virtual string Text
{
get
{
string s = (string)ViewState["Text"];
return((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
}
protected override void TrackViewState()
{
base.TrackViewState();
}
protected override void LoadControlState(object savedState)
{
base.LoadControlState(savedState);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
protected override object SaveControlState()
{
return base.SaveControlState();
}

protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Input;
}
}
[
Category("Action"),
Description("Raised when the button is clicked")
]
public event EventHandler Click;

protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Name,this.UniqueID);
writer.AddAttribute(HtmlTextWriterAttribute.Type,"Submit");
writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
}
// Method of IPostBackEventHandler that raises postback events.
void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
{
OnClick(EventArgs.Empty);
}
// Invokes delegate registered with the Click event.
protected virtual void OnClick(EventArgs e)
{ 
if (Click != null)
{
Click(this, e);
}
}

protected override void Render(HtmlTextWriter writer)
{
// Ensures that this control is nested in a server form.
if (Page != null)
{
Page.VerifyRenderingInServerForm(this);
}
base.Render(writer);
}
}
让我们看看客户端单击事件是如何发送到服务器的:
首先,让我们从页面被编译成类似App_web_tingluzi.dll的临时文件开始说起,在此之前Application内部类CallHandlerExecutionStep的Excute()方法执行后,会转到Page的生命期。经过一系列的ProcessRequest、编译(JIT)App_web_tingluzi.dll临时文件,然后就会编译(JIT)我们写的控件的程序集了本例中是SimpleButton.dll,正式进入控件的生命期。(如果有Global,会在web页前被编译为Global.dll)
如果想了解详细的信息可以看dudu的:
解读System.Web.UI.Page中关键方法ProcessRequestMain()
注:基于1.1,不过基本的东西还是一样的
/html/edu/net/net1/13538.html
ASP.NET 2.0运行时简要分析
/html/edu/net/net1/9631.html
客户端事件映射到服务器端事件的流程:
从Page的核心方法ProcessRequestMain()开始,它处理Page的整个生命期(详细的可看dudu的分析)。
现在只关注和回传有关的内容,下面是涉及到回发的页面生命期,从Init结束开始:
1、LoadAllState
(包含LoadPageStateFromPersistenceMedium,LoadViewStateRecursive)
2、ProcessPostData //通过判断控件是否实现PostBackEventHandler标记其是否引发Postback
3、PreLoad(非PostBack)
4、Load(非PostBack)
5、ProcessPostData
6、RaiseChangedEvents
7、RaisePostBackEvent//通过显示接口调用客户端的RaisePostBackEvent,并且实现了两类绑定:UniqID和客户端脚步
下面深入的了解一下Pages是如何实现自动映射的:
首先是ProcessPostData方法,他保证了回传能够正确的进行,是回传的基础:
下面是有关ProcessPostData的伪代码:
参数说明:postDate:Form回发给服务器的内容包含form中引起回传的控件、RequestQury的值、_VIEWSTATE、以及通过脚本回传给服务期的数据。通过Page.RequestValueCollection可以看到集合的值。
//postData指:Page.RequestValueCollection。bool标记是Load前调用的还是load后调用的
private IPostBackEventHandler _registeredControlThatRequireRaiseEvent;
private void ProcessPostData(NameValueCollection postData, bool fBeforeLoad)
IPostBackDataHandler handler1 = control1.PostBackDataHandler;
if (handler1 == null)
{
if (control1.PostBackEventHandler != null)
{
this.RegisterRequiresRaiseEvent(control1.PostBackEventHandler);
}
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
public virtual void RegisterRequiresRaiseEvent(IPostBackEventHandler control)
{
this._registeredControlThatRequireRaiseEvent = control;
}