[JavaScript]自定义MessageBox续-全面分析PostBack之Client Script

全面分析PostBack之Client Script


 -Written by 浪子@cnblogs.com (2006.08.14)

问题描述:

 

   继上文[JavaScript]自定义MessageBox ,我还剩下一个问题没有解决,"如何获取control的完整的客户端代码"?。所以本文解决的问题只涉及PostBack的客户端Script,而不涉及服务端的PostBack机制如何运作。

 

问题分析:

  

   PostBack2种方式

1、  通过__doPostBack(eventTarget, eventArgument),例如LinkButton;

2、  通过formsubmit方式,例如采用UseSubmitBehaviorButton

   

   对于button,可能会有三种绘制方式:

1、  buttonUseSubmitBehaviortrue,并且没有触发验证事件;

采用formsubmit方式,不需要描绘script

2、  buttonUserSubmitBehaviorfalse,并且没有触发验证事件;

采用__doPostBack函数

3、  button有触发验证事件:

采用WebForm_DoPostBackWithOptions(options)函数


ExpandedBlockStart.gif ContractedBlock.gif function  WebForm_PostBackOptions(eventTarget, eventArgument, validation, validationGroup, actionUrl, trackFocus, clientSubmit)  dot.gif {
InBlock.gif    
this.eventTarget = eventTarget;
InBlock.gif    
this.eventArgument = eventArgument;
InBlock.gif    
this.validation = validation;
InBlock.gif    
this.validationGroup = validationGroup;
InBlock.gif    
this.actionUrl = actionUrl;
InBlock.gif    
this.trackFocus = trackFocus;
InBlock.gif    
this.clientSubmit = clientSubmit;
ExpandedBlockEnd.gif}

None.gif 
ExpandedBlockStart.gifContractedBlock.gif
function  WebForm_DoPostBackWithOptions(options)  dot.gif {
InBlock.gif    
var validationResult = true;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (options.validation) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (typeof(Page_ClientValidate) == 'function') dot.gif{
InBlock.gif            validationResult 
= Page_ClientValidate(options.validationGroup);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (validationResult) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if ((typeof(options.actionUrl) != "undefined"&& (options.actionUrl != null&& (options.actionUrl.length > 0)) dot.gif{
InBlock.gif            theForm.action 
= options.actionUrl;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (options.trackFocus) dot.gif{
InBlock.gif            
var lastFocus = theForm.elements["__LASTFOCUS"];
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if ((typeof(lastFocus) != "undefined"&& (lastFocus != null)) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (typeof(document.activeElement) == "undefined"dot.gif{
InBlock.gif                    lastFocus.value 
= options.eventTarget;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockStart.gifContractedSubBlock.gif                
else dot.gif{
InBlock.gif                    
var active = document.activeElement;
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if ((typeof(active) != "undefined"&& (active != null)) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
if ((typeof(active.id) != "undefined"&& (active.id != null&& (active.id.length > 0)) dot.gif{
InBlock.gif                            lastFocus.value 
= active.id;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockStart.gifContractedSubBlock.gif                        
else if (typeof(active.name) != "undefined"dot.gif{
InBlock.gif                            lastFocus.value 
= active.name;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (options.clientSubmit) dot.gif{
InBlock.gif        __doPostBack(options.eventTarget, options.eventArgument);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif



 

 

 

问题解决:

 

   我需要的是采集PostBackClient Script,所以只需要获取__doPostBack或者WebForm_DoPostBackWithOptionsoptions)函数,以便传给KMessageBox进行事件挂载。

 

   按上面的分析我们已经知道什么情况下使用__doPostBack,什么情况下使用WebForm_DoPostBackWithOptionsoptions)。

  分析一下Reflector出来的Button源码:
 

ContractedBlock.gif ExpandedBlockStart.gif OnPreRender
 1None.gif if ((this.Page != null&& base.IsEnabled)
 2ExpandedBlockStart.gifContractedBlock.gif      dot.gif{
 3InBlock.gif            if ((this.CausesValidation && (this.Page.GetValidators(this.ValidationGroup).Count > 0)) || !string.IsNullOrEmpty(this.PostBackUrl))
 4ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 5InBlock.gif                  this.Page.RegisterWebFormsScript();
 6ExpandedSubBlockEnd.gif            }

 7InBlock.gif            else if (!this.UseSubmitBehavior)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 9InBlock.gif                  this.Page.RegisterPostBackScript();
10ExpandedSubBlockEnd.gif            }

11ExpandedBlockEnd.gif      }

12None.gif

    有了这段代码,我们就可以很清晰的得到服务端如何决定采取什么方式来绘制客户端的PostBack Script了

ContractedBlock.gif ExpandedBlockStart.gif DoPostBackWithOptions
1ExpandedBlockStart.gifContractedBlock.gifprivate string gDoPostBackWithOptions = "WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(\"dot.gif{0}\", \"dot.gif{1}\", {2}, \"dot.gif{3}\", \"dot.gif{4}\", {5}, {6}))";

   
None.gif IButtonControl button  =  (IButtonControl)control;
None.gif
None.gif                    
if  ((button.CausesValidation  &&  ( this .Page.GetValidators(button.ValidationGroup).Count  >   0 ))  ||   ! string .IsNullOrEmpty(button.PostBackUrl))
ExpandedBlockStart.gifContractedBlock.gif                    
dot.gif {
InBlock.gif                        strOnClickScript 
= string.Format(gDoPostBackWithOptions, control.ClientID, button.CommandArgument, button.CausesValidation.ToString().ToLower(), button.ValidationGroup, button.PostBackUrl, "false""true");
ExpandedBlockEnd.gif                    }

None.gif                    
else
ExpandedBlockStart.gifContractedBlock.gif                    
dot.gif {
InBlock.gif                        strOnClickScript 
= Page.ClientScript.GetPostBackEventReference(control, button.CommandArgument);
ExpandedBlockEnd.gif                    }


     至此KMessageBox服务端的封装工作全部结束。做到,不影响原来的编码方式,只需要简单的执行一下KMessageBox的RegisterConfrim函数为control注册一下客户端事件就搞定一切。

转载于:https://www.cnblogs.com/walkingboy/archive/2006/08/14/PostBack_ClientScript.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值