全面分析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)函数
this.eventTarget = eventTarget;
this.eventArgument = eventArgument;
this.validation = validation;
this.validationGroup = validationGroup;
this.actionUrl = actionUrl;
this.trackFocus = trackFocus;
this.clientSubmit = clientSubmit;
}
function WebForm_DoPostBackWithOptions(options) {
var validationResult = true;
if (options.validation) {
if (typeof(Page_ClientValidate) == 'function') {
validationResult = Page_ClientValidate(options.validationGroup);
}
}
if (validationResult) {
if ((typeof(options.actionUrl) != "undefined") && (options.actionUrl != null) && (options.actionUrl.length > 0)) {
theForm.action = options.actionUrl;
}
if (options.trackFocus) {
var lastFocus = theForm.elements["__LASTFOCUS"];
if ((typeof(lastFocus) != "undefined") && (lastFocus != null)) {
if (typeof(document.activeElement) == "undefined") {
lastFocus.value = options.eventTarget;
}
else {
var active = document.activeElement;
if ((typeof(active) != "undefined") && (active != null)) {
if ((typeof(active.id) != "undefined") && (active.id != null) && (active.id.length > 0)) {
lastFocus.value = active.id;
}
else if (typeof(active.name) != "undefined") {
lastFocus.value = active.name;
}
}
}
}
}
}
if (options.clientSubmit) {
__doPostBack(options.eventTarget, options.eventArgument);
}
}
问题解决:
我需要的是采集PostBack的Client Script,所以只需要获取__doPostBack或者WebForm_DoPostBackWithOptions(options)函数,以便传给KMessageBox进行事件挂载。
按上面的分析我们已经知道什么情况下使用__doPostBack,什么情况下使用WebForm_DoPostBackWithOptions(options)。
分析一下Reflector出来的Button源码:
1 if ((this.Page != null) && base.IsEnabled)
2 {
3 if ((this.CausesValidation && (this.Page.GetValidators(this.ValidationGroup).Count > 0)) || !string.IsNullOrEmpty(this.PostBackUrl))
4 {
5 this.Page.RegisterWebFormsScript();
6 }
7 else if (!this.UseSubmitBehavior)
8 {
9 this.Page.RegisterPostBackScript();
10 }
11 }
12
有了这段代码,我们就可以很清晰的得到服务端如何决定采取什么方式来绘制客户端的PostBack Script了
1private string gDoPostBackWithOptions = "WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(\"{0}\", \"{1}\", {2}, \"{3}\", \"{4}\", {5}, {6}))";
if ((button.CausesValidation && (this.Page.GetValidators(button.ValidationGroup).Count > 0)) || !string.IsNullOrEmpty(button.PostBackUrl))
{
strOnClickScript = string.Format(gDoPostBackWithOptions, control.ClientID, button.CommandArgument, button.CausesValidation.ToString().ToLower(), button.ValidationGroup, button.PostBackUrl, "false", "true");
}
else
{
strOnClickScript = Page.ClientScript.GetPostBackEventReference(control, button.CommandArgument);
}
至此KMessageBox服务端的封装工作全部结束。做到,不影响原来的编码方式,只需要简单的执行一下KMessageBox的RegisterConfrim函数为control注册一下客户端事件就搞定一切。
http://www.cnblogs.com/walkingboy/archive/2006/08/14/PostBack_ClientScript.html