在Asp.net中利用Javascript避免表单重复提交(for .net2.0)

来源:互联网 发布:windows一键安装 编辑:程序博客网 时间:2024/05/29 14:02
 
  1. //---------------------------------------------------------
  2. // 作者:野文(Jasson)
  3. // 日期:2007-1-11
  4. // 功能:避免重复提交数据
  5. //       请放在HTML页面的最后,</body>标记前</form>标记后。
  6. //---------------------------------------------------------
  7. function _doPostBack(){}; 
  8. if(typeof(__doPostBack)=="function"
  9. {     
  10.     _doPostBack=__doPostBack; 
  11.     __doPostBack=_doPostBackNew; 
  12. document.attachEvent("onmousemove",_onmousemove); 
  13. var _isPosting=false
  14. var _divMask=null
  15. var _divMOMask = null;
  16. function _onmousemove() 
  17. {       
  18.     if(_divMOMask) 
  19.         with(_divMOMask.runtimeStyle) 
  20.         {
  21.           left=event.clientX+document.body.scrollLeft-4; 
  22.           top=event.clientY+document.body.scrollTop-4; 
  23.         } 
  24. }
  25. function _makeMOMask() 
  26.     var div=document.createElement("DIV");   
  27.     with(div.runtimeStyle) 
  28.     {
  29.         width="100%";
  30.         height="100%";
  31.         position="absolute";      
  32.       zIndex=999999; 
  33.       fontSize="12px";
  34.       cursor="wait"
  35.       backgroundColor="gray"
  36.       filter="alpha(opacity=0)";               
  37.     } 
  38.     try 
  39.     {         
  40.       document.body.insertAdjacentElement("BeforeEnd",div);  
  41.       div.style.left=( parseFloat(document.body.offsetWidth)-parseFloat(div.offsetWidth))/2  ;//event.clientX+document.body.scrollLeft-4; 
  42.       div.style.top=(  parseFloat(document.body.offsetHeight)-parseFloat(div.offsetHeight))/2;//event.clientY+document.body.scrollTop-4;                   
  43.       div.onblur=new Function("this.focus()"); 
  44.       div.focus();       
  45.     } 
  46.     catch(x){} 
  47.     if(_divMOMask)    
  48.         _divMOMask.removeNode(true); 
  49.      _divMOMask=div; 
  50. }  
  51. function _makeMask() 
  52.     var div=document.createElement("DIV"); 
  53.     
  54.     with( div )
  55.     {
  56.         innerHTML = "<font color='red'>正在更新页面,请稍候...</font>";    
  57.         align="center";      
  58.   }
  59.   
  60.     with(div.runtimeStyle) 
  61.     {
  62.         paddingLeft="30px";
  63.         paddingRight="30px";
  64.         paddingTop = "15px";
  65.         paddingBottom="15px";
  66.         position="absolute";      
  67.       zIndex=999998; 
  68.       fontSize="12px";
  69.       cursor="wait"
  70.       backgroundColor="#D0D0FF"
  71.       filter="alpha(opacity=90)";               
  72.     } 
  73.     try 
  74.     {         
  75.       document.body.insertAdjacentElement("BeforeEnd",div);  
  76.       div.style.left=( parseFloat(document.body.offsetWidth)-parseFloat(div.offsetWidth))/2  ;//event.clientX+document.body.scrollLeft-4; 
  77.       div.style.top=(  parseFloat(document.body.offsetHeight)-parseFloat(div.offsetHeight))/2;//event.clientY+document.body.scrollTop-4;                   
  78.       //div.onblur=new Function("this.focus()");       
  79.       //div.focus();       
  80.     } 
  81.     catch(x){} 
  82.     if(_divMask)    
  83.         _divMask.removeNode(true); 
  84.      _divMask=div; 
  85. function _doPostBackNew(sender,args) 
  86.     if(_isPosting) 
  87.       return event.returnValue=!(event.cancelBubble=true); 
  88.     window.status="正在更新页面,请稍候...";        
  89.     _doPostBack(sender,args); 
  90.     _isPosting=true
  91.     _makeMask();
  92.     _makeMOMask(); 
  93. function _onformsubmit() 
  94.     if(_isPosting) 
  95.       return event.returnValue=!(event.cancelBubble=true); 
  96.     _isPosting=true
  97.     _makeMask(); 
  98.     _makeMOMask();   
  99. new function _attachForms() 
  100. {
  101.     iftypeof(WebForm_OnSubmit)=="function" )
  102.     {
  103.         function _WebForm_OnSubmit(){};
  104.         _WebForm_OnSubmit = WebForm_OnSubmit;
  105.         WebForm_OnSubmit = function()
  106.         {
  107.             if( _WebForm_OnSubmit() )
  108.                 _onformsubmit();
  109.         }        
  110.     }
  111.      
  112.     with(new Enumerator(document.forms)) 
  113.     for(;!atEnd();moveNext()) 
  114.     { 
  115.         iftypeof(item().onsubmit)!="function")
  116.           item().attachEvent("onsubmit",_onformsubmit);       
  117.       var div=document.createElement("div"); 
  118.       div.runtimeStyle.width="0px"
  119.       div.runtimeStyle.hight="0px"
  120.       div.runtimeStyle.overflow="hidden"
  121.       div.runtimeStyle.position="absolute"
  122.       item(0).insertAdjacentElement("afterbegin",div); 
  123.       div.innerHTML="<INPUT id=webformpatchsubmitelement onclick='return event.returnValue=false' type=submit value=webformpatchsubmitelement name=webformpatchsubmitelement>";     
  124.     } 
原创粉丝点击