两个基本概念:
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);
}
}
//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;
}
private void RaisePostBackEvent(NameValueCollection postData)
{
//ProcessPostData已经把实现PostBackEventHandler接口的控件标记为可PostBack了
if (this._registeredControlThatRequireRaiseEvent != null)
{
//通过UniqID映射
this.RaisePostBackEvent(this._registeredControlThatRequireRaiseEvent, null);
}
else
{
//通过脚步回传映射
string text1 = postData["__EVENTTARGET"];
bool flag1 = !string.IsNullOrEmpty(text1);
if (flag1 || (this.AutoPostBackControl != null))
{
Control control1 = null;
if (flag1)
{来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-332341/ ,如需转载,请注明出处,否则将追究法律责任。