防刷新.

一.不错的方法:
首先.网上关于防刷新的很多. 通用性强的不多,其中有一篇,说出了方法.但是没起到效果. http://www.cnblogs.com/lizhimin/archive/2007/04/24/725337.html

它是根据: F5 或刷新 不会刷新 客户端 的 Button Click 脚本. 这种方法,当Button 连续点两次,Button 的Click 就不起作用了. 而且. Session无辜的浪费了服务器内存.
用法:
            if (IsRefreshed == false )
            
{
                
this.Label1.Text = DateTime.Now.ToString();
            }



二. 更简单的方法:
Response.Write("<script>window.location = window.location;</script>");

这种方法也有适用性: 当在当前页面显示出错信息时,或输出内容时, 就不行了. 但是, 可以用 alert 方式.

三. VS2008 下, 可以用 Ajax1.0 来防刷新. 其实,原理是利用了 不会刷新 客户端脚本.

只是该方法只能更新 UpdatePannel 里的控件内容, 无法使用 Response 或 UpdatePannel 控件外面的值 .

尝试的方法失败: 还是利用 客户端不会刷新:

写一个 Javascript 来提交. 客户端用 HTML 元素来显示操作. 用 服务器端控件来执行方法( 服务器.syle.diplay = "none" )  .

function __doPostBack_Ex(eventTarget, eventArgument) 
{
    
var theform;
    
if (window.navigator.appName.toLowerCase().indexOf("netscape"> -1{
        theform 
= document.forms[0];
    }

    
else {
        theform 
= document.forms[0];
    }


    
if(!theform.__EVENTTARGET)
    
{            
        theform.appendChild(document.createElement(
"<input type='hidden' name='__EVENTTARGET'>"));
    }

    
    
if(!theform.__EVENTARGUMENT)
    
{            
        theform.appendChild(document.createElement(
"<input type='hidden' name='__EVENTARGUMENT'>"));                        
    }

    
    theform.__EVENTTARGET.value 
= eventTarget.split("$").join(":");
    theform.__EVENTARGUMENT.value 
= eventArgument;
    
if ((typeof(theform.onsubmit) == "function")) 
    
{
        
if(theform.onsubmit()!=false)
        
{
            theform.submit();    
        }

    }

    
else
    
{            
        theform.submit();    
    }

}

客户端的 Button 里写.
__doPostBack_Ex(document.getElementById("服务器端控件ID").name) ;

这样, 还是不能防止刷新.因为. 还是变相的采用提交.

看来:
从适用性上来说. 第二种最好.
从便利性上来说. 第三种最好.


posted @ 2008-03-16 21:35  NewSea  阅读(839)  评论(0编辑  收藏  举报