继上文[JavaScript]自定义MessageBox ,我还剩下一个问题没有解决,即"如何获取control的完整的客户端代码"?。
本文将全面解析Asp.Net的PostBack的客户端Script如何绘制。
(本文的问题只涉及PostBack的客户端Script,而不涉及服务端的PostBack机制如何运作。)
全面分析PostBack之Client Script
-Written by 浪子@cnblogs.com (2006.08.14)
问题描述:
继上文[JavaScript]自定义MessageBox ,我还剩下一个问题没有解决,即"如何获取control的完整的客户端代码"?。所以本文解决的问题只涉及PostBack的客户端Script,而不涉及服务端的PostBack机制如何运作。
问题分析:
PostBack有2种方式
1、 通过__doPostBack(eventTarget, eventArgument),例如LinkButton;
2、 通过form的submit方式,例如采用UseSubmitBehavior的Button
对于button,可能会有三种绘制方式:
1、 button的UseSubmitBehavior为true,并且没有触发验证事件;
采用form的submit方式,不需要描绘script
2、 button的UserSubmitBehavior为false,并且没有触发验证事件;
采用__doPostBack函数
3、 button有触发验证事件:
采用WebForm_DoPostBackWithOptions(options)函数
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
function WebForm_PostBackOptions(eventTarget, eventArgument, validation, validationGroup, actionUrl, trackFocus, clientSubmit)
{
this.eventTarget = eventTarget;
this.eventArgument = eventArgument;
this.validation = validation;
this.validationGroup = validationGroup;
this.actionUrl = actionUrl;
this.trackFocus = trackFocus;
this.clientSubmit = clientSubmit;
}
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
function WebForm_DoPostBackWithOptions(options)
{
var validationResult = true;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (options.validation)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (typeof(Page_ClientValidate) == 'function')
{
validationResult = Page_ClientValidate(options.validationGroup);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (validationResult)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((typeof(options.actionUrl) != "undefined") && (options.actionUrl != null) && (options.actionUrl.length > 0))
{
theForm.action = options.actionUrl;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (options.trackFocus)
{
var lastFocus = theForm.elements["__LASTFOCUS"];
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((typeof(lastFocus) != "undefined") && (lastFocus != null))
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (typeof(document.activeElement) == "undefined")
{
lastFocus.value = options.eventTarget;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
var active = document.activeElement;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((typeof(active) != "undefined") && (active != null))
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((typeof(active.id) != "undefined") && (active.id != null) && (active.id.length > 0))
{
lastFocus.value = active.id;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if (typeof(active.name) != "undefined")
{
lastFocus.value = active.name;
}
}
}
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (options.clientSubmit)
{
__doPostBack(options.eventTarget, options.eventArgument);
}
}
![](/Images/OutliningIndicators/None.gif)
问题解决:
我需要的是采集PostBack的Client Script,所以只需要获取__doPostBack或者WebForm_DoPostBackWithOptions(options)函数,以便传给KMessageBox进行事件挂载。
按上面的分析我们已经知道什么情况下使用__doPostBack,什么情况下使用WebForm_DoPostBackWithOptions(options)。
分析一下Reflector出来的Button源码:
![](/Images/OutliningIndicators/ContractedBlock.gif)
OnPreRender
1
if ((this.Page != null) && base.IsEnabled)
2![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
if ((this.CausesValidation && (this.Page.GetValidators(this.ValidationGroup).Count > 0)) || !string.IsNullOrEmpty(this.PostBackUrl))
4![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
5
this.Page.RegisterWebFormsScript();
6
}
7
else if (!this.UseSubmitBehavior)
8![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
this.Page.RegisterPostBackScript();
10
}
11
}
12![](/Images/OutliningIndicators/None.gif)
有了这段代码,我们就可以很清晰的得到服务端如何决定采取什么方式来绘制客户端的PostBack Script了
![](/Images/OutliningIndicators/ContractedBlock.gif)
DoPostBackWithOptions
1![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
private string gDoPostBackWithOptions = "WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(\"
{0}\", \"
{1}\", {2}, \"
{3}\", \"
{4}\", {5}, {6}))";
IButtonControl button = (IButtonControl)control;
![](/Images/OutliningIndicators/None.gif)
if ((button.CausesValidation && (this.Page.GetValidators(button.ValidationGroup).Count > 0)) || !string.IsNullOrEmpty(button.PostBackUrl))
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
strOnClickScript = string.Format(gDoPostBackWithOptions, control.ClientID, button.CommandArgument, button.CausesValidation.ToString().ToLower(), button.ValidationGroup, button.PostBackUrl, "false", "true");
}
else
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
strOnClickScript = Page.ClientScript.GetPostBackEventReference(control, button.CommandArgument);
}
至此KMessageBox服务端的封装工作全部结束。做到,不影响原来的编码方式,只需要简单的执行一下KMessageBox的RegisterConfrim函数为control注册一下客户端事件就搞定一切。