突發奇想,如下:
編寫自定義Button一個,服務器端的,當我點按鈕執行事件的時候,事件可能會運行一段時間,我要出來一個DIV覆蓋整個頁面,設置半透明,上面顯示一個進度的GIF圖片,下面提示Please wait...
我試過寫出來,是輸出腳本的,效果不理想.
有沒有人做過類似的事情啊,能留下代碼就萬分感謝!

解决方案 »

  1.   

     var virtualPath;
            function show(posY,imgPath, mnuTitle, dialogString, btnCount, path, ascxPath)
            {
            if(window.screen.Height>=document.body.scrollHeight)
                {
                id1.style.height = (window.screen.height - 155) +"px";
                }
                else
            {
            id1.style.height = (document.body.scrollHeight) + "px";
        }
                id1.style.width = (window.screen.width - 25) + "px";
                id2.style.left = (window.screen.width * 0.5) - 170;
                id1.style.display = 'block';
                id2.style.display = 'block'
                id2.style.top=posY;
                MenuTitle.innerText = mnuTitle;
                DialogString.innerText = dialogString;
                infor = document.getElementById("infor");
                infor.src = imgPath;
                
                var dialogOkImg = document.getElementById(ascxPath);
                var dialogCancelImg = document.getElementById("dialogCancelImg");
                if(btnCount == "1")
                {
                    dialogOkImg.style.display = 'inline'
                    dialogCancelImg.style.display = "none";
                }
                else
                {
                    dialogOkImg.style.display = "inline";
                    dialogCancelImg.style.display = "inline";
                }
                var hda = document.getElementById("hda");
                hda.value = path;
                return false;
            }
    这是ajax书上的核心代码,其余的靠你自己想了
      

  2.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">
    <head>
    <meta name="verify-v1" content="P4T6fFCiPVxqMWZ2eztyXVzMHlnwD0wLQOq6LBHN5Y8=" />
    <title>
    政策文件_中国电力工程造价信息网</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <meta http-equiv="Content-Language" content="gb2312" />
    <meta name="KeyWords" content="电力工程造价,价格信息,装材,设备,电力工程" />
    <meta name="description" content="电力工程造价信息" />
    <style type="text/css">
        *{
            margin:0;padding:0;
        }
    </style>
    </head>
    <body>
    <div class="cecmbody" id="cecmpolicy">
    <div class="leftClass">
    <input type="button" value="点击这里" onclick="sAlert('测试效果');" />
    </div>
    <div class="rightClass">
    </div>
    </div>
    <script type="text/javascript" language="javascript">//Author:Daviv
    //Blog:http://blog.163.com/jxdawei
    //Date:2006-10-27
    //Email:[email protected] function sAlert(str){
    var msgw,msgh,bordercolor;
    msgw=400;//提示窗口的宽度
    msgh=100;//提示窗口的高度
    titleheight=25 //提示窗口标题高度
    bordercolor="#336699";//提示窗口的边框颜色
    titlecolor="#99CCFF";//提示窗口的标题颜色

    var sWidth,sHeight;
    sWidth=document.body.offsetWidth;
    sHeight=screen.height; var bgObj=document.createElement("div");
    bgObj.setAttribute('id','bgDiv');
    bgObj.style.position="absolute";
    bgObj.style.top="0";
    bgObj.style.background="#777";
    bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
    bgObj.style.opacity="0.6";
    bgObj.style.left="0";
    bgObj.style.width=sWidth + "px";
    bgObj.style.height=sHeight + "px";
    bgObj.style.zIndex ="1000";
    document.body.appendChild(bgObj);

    var msgObj=document.createElement("div")
    msgObj.setAttribute("id","msgDiv");
    msgObj.setAttribute("align","center");
    msgObj.style.background="white";
    msgObj.style.border="1px solid " + bordercolor;
         msgObj.style.position = "absolute";
                msgObj.style.left = "50%";
                msgObj.style.top = "50%";
                msgObj.style.font="12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
                msgObj.style.marginLeft = "-225px" ;
                msgObj.style.marginTop = -75+document.documentElement.scrollTop+"px";
                msgObj.style.width = msgw + "px";
                msgObj.style.height =msgh + "px";
                msgObj.style.textAlign = "center";
                msgObj.style.lineHeight ="25px";
                msgObj.style.zIndex = "10000001";
       
       var title=document.createElement("h4");
       title.setAttribute("id","msgTitle");
       title.setAttribute("align","right");
       title.style.margin="0";
       title.style.padding="3px";
       title.style.background=bordercolor;
       title.style.filter="progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
       title.style.opacity="0.75";
       title.style.border="1px solid " + bordercolor;
       title.style.height="18px";
       title.style.font="12px Verdana, Geneva, Arial, Helvetica, sans-serif";
       title.style.color="white";
       title.style.cursor="pointer";
       title.innerHTML="关闭";
       title.onclick=function(){
            document.body.removeChild(bgObj);
                    document.getElementById("msgDiv").removeChild(title);
                    document.body.removeChild(msgObj);
                    }
       document.body.appendChild(msgObj);
       document.getElementById("msgDiv").appendChild(title);
       var txt=document.createElement("p");
       txt.style.margin="1em 0"
       txt.setAttribute("id","msgTxt");
       txt.innerHTML=str;
               document.getElementById("msgDiv").appendChild(txt);
                }
    </script>
    </body>
    </html>
      

  3.   


    public void RegisterAdminPageClientScriptBlock()
    {
    string style = Style=="xp"?"#D3DBDE;":"#9FC4FB;";
    string script = "<script language=\"javascript\"> \r\n" +
    "var spath=''\r\n" +
    "var bar=0\r\n" +
    "function count() \r\n" +
    "{  \r\n" +
    " bar=bar+1; \r\n" +
    " if (bar<10) {setTimeout(\"count()\",2000);}  \r\n" +
    " else { document.getElementById('success').style.display =\"none\"; }  \r\n" +
    "} \r\n" +
    "function run(spath) \r\n" +
    "{ \r\n" +
    " if (typeof(Page_ClientValidate) == 'function') \r\n" +
    " { \r\n" +
    " if(Page_ClientValidate()) \r\n" +
    " { \r\n" +
    " document.getElementById('Layer5').innerHTML='<BR /><table><tr><td valign=top><img border=\"0\" src='+spath+'  /></td><td valign=middle style=\"font-size: 14px;\" >"+RM.GetString("OpMessage")+"<BR /></td></tr></table><BR />';\r\n" +
    " document.getElementById('success').style.display = \"block\"; \r\n" +
    " setInterval('count()',2000); \r\n" +
    " } \r\n" +
    " } \r\n" +
    " else \r\n" +
    " { \r\n" +
    " document.getElementById('Layer5').innerHTML='<BR /><table><tr><td valign=top><img border=\"0\" src='+spath+'  /></td><td valign=middle style=\"font-size: 14px;\" >"+RM.GetString("OpMessage")+"<BR /></td></tr></table><BR />';\r\n" +
    " document.getElementById('success').style.display = \"block\"; \r\n" +
    " setInterval('count()',2000); \r\n" +
    " } \r\n" +
    "} \r\n" +
    "</script>\r\n"+
    "<div id=\"success\" style=\"position:absolute;z-index:300;height:120px;width:284px;left:50%;top:50%;margin-left:-150px;margin-top:-80px;\">\r\n" +
    " <div id=\"Layer2\" style=\"position:absolute;z-index:300;width:270px;height:90px;background-color: #FFFFFF;border:solid #000000 1px;font-size:14px;\">\r\n" +
    " <div id=\"Layer4\" style=\"height:26px;background:"+style+"line-height:26px;padding:0px 3px 0px 3px;font-weight:bolder;\">"+RM.GetString("OpTitle")+"</div>\r\n" +
    " <div id=\"Layer5\" style=\"height:64px;line-height:150%;padding:0px 3px 0px 3px;\" align=\"center\"><BR /><table><tr><td valign=top><img border=\"0\" src=\"../images/ajax_loading.gif\"  /></td><td valign=middle style=\"font-size: 14px;\" >"+RM.GetString("OpMessage")+"<BR /></td></tr></table><BR /></div>\r\n" +
    " </div>\r\n" +
    " <div id=\"Layer3\" style=\"position:absolute;width:270px;height:90px;z-index:299;left:4px;top:5px;background-color: #E8E8E8;\"></div>\r\n" +
    "</div>\r\n" +
    "<script> \r\n" +
    "document.getElementById('success').style.display = \"none\"; \r\n" +
    "</script> \r\n" ;
    //"<script language=\"JavaScript1.2\" src=\"../js/divcover.js\"></script>\r\n"; base.RegisterClientScriptBlock("Page", script);
    }
    //以上代码个别地方需要修改,是变量
    //页面加载时调用上述方法,例如:
    private void Page_Load(object sender, System.EventArgs e)
    {
    RegisterAdminPageClientScriptBlock();
    }
    //在要使用的控件上加<A href="#" onclick='run("图片路径")'><asp:button id="btnAddNew" runat="server" Text="新增" Width="70px" CausesValidation="False"></asp:button></A>
      

  4.   

    謝謝,不過我想寫的是一個控件
    我能用JS寫出來這個效果,不過我不想每個控件,頁面都寫,這樣很麻煩
    所以我想把它寫成個Button控件
      

  5.   

    转自 http://www.never-online.net/blog/article.asp?id=22
    保存为html网页   打开后点击按钮 "Excute it" 查看效果<?xml version="1.0" encoding="UTF-8"?> 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
     <title> neverDialog - http://www.never-online.net </title>
     <meta http-equiv="ImageToolbar" content="no" />
     <meta name="author" content="BlueDestiny, never-online"/>
     <meta name="keywords" content="never modules, Mozilla CSS, C#, .net, Refercence, BlueDestiny, never-online, www.never-online.net"/>
     <meta name="description" content="BlueDestiny, never-online"/>
     <meta name="title" content="neverDialog - http://www.never-online.net" />
     <meta name="creator.name" content="Genius Lau, never-online, blueDestiny" />
     <style type="text/css" media="all" title="Default">
     body { background-color:appworkspace; margin:0; padding:0; }
     .button { font-size:9pt; font-family:verdana; border-left:4px solid #444; border-top:1px solid #444; border-bottom:1px solid #444; border-right:1px solid #444; margin:20px; }
     </style>
     <script type="text/javascript">
     //<![CDATA[
     function neverDialog(event, msg, handler, notfilter) {
     // Script by BlueDestiny, never-online
     // Simple simulate modal dialog
     // welcome to http://blog.never-online.net
     // http://blog.csdn.net/BlueDestiny/
     // and keep these copyright
     window._timer = null;
     var ie = document.all?true:false; 
     if (event) event.srcElement ? event.srcElement.blur() : event.target.blur();
     if (_timer) window.clearTimeout(_timer);
     function getElById(idStr) { return document.getElementById(idStr); }
     var d = getElById("dialog");
     var a = getElById("demo");
     var Htmls = "";
     var cnt = 20;
     Htmls += "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
     + "<head>\n"
     + "<style text=\"text/css\">\n"
     + "body { margin:40px; background-color:#eeeeee; }\n"
     + "* { font-size:9pt; font-family:verdana; }\n"
     + ".bold { font-weight:bold; }\n"
     + ".button { width:100px; border-left:4px solid #444; border-top:1px solid #444; border-bottom:1px solid #444; border-right:1px solid #444; }\n"
     + "</style>\n"
     + "</head>\n"
     + "<body>\n"
     + "<p align='center'>never dialog demo, just a test, http://blog.never-online.net</p>\n"
     + "<p align='center' class='bold'>" +msg+ "</p>\n"
     + "<p align='center'>\n"
     + "<input class='button' type='button' value=' - O K - ' onclick='top.window.document.getElementById(\"demo\").style.display=\"none\";" +(handler?";top.window."+handler+"":"")+ "\'/>\n"
     + "<input type='button' class='button' value=' - N O -' onclick='top.window.document.getElementById(\"demo\").style.display=\"none\";top.window.document.body.style.overflow=\"\"'/>"
     + "</p>\n"
     + "</body>\n"
     + "</html>"
     d.contentWindow.document.open("text/html");
     d.contentWindow.document.write(Htmls);
     d.contentWindow.document.close();
     with(a.style) {
     display = "block";
     left="0px";
     top="0px";
      margin="0px";
     width=(document.body.clientWidth||window.innerWidth)+"px";
     height=(document.body.scrollHeight+25)+"px";
     }
     function f_alpha() {
     if (++cnt>=80) {
     window.clearTimeout(_timer);
     return;
     }
     ie?a.style.filter="alpha(opacity=" +cnt+")":a.style.MozOpacity=(cnt/100)+"";
     _timer = window.setTimeout(f_alpha, 10);
     }; notfilter?'':f_alpha();
     }
     function handler(event) {
     neverDialog(event,'welcome to http://blog.never-online.net');
     }
     //]]>
     </script>
    </head>
    <body id="www.never-online.net">
    <input type="button" value="Excute it" onclick="neverDialog(event,'Are You Sure To Excute it?','handler(event)')" class="button"/>
    <div height="100%" width="100%" style="display:none;position:absolute;-moz-opacity:0.6" id="demo">
    <iframe id="dialog" height="100%" width="100%" frameborder="0" marginheight="0" marginwidth="0" style="font-size:9pt; font-family:verdana;">
    </iframe>
    </div><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p><p> &amp;nbsp; </p>
    </body>
    </html>
      

  6.   

    謝謝,謝謝大家幫頂.
    但是你給我看的效果都基本是針對HTMLButton
    但是如果我有幾個服務器端控件,而且我要的效果只能在事件執行的時候出現
    那最好的辦法就是自己寫一個控件,要不然就要重複的寫很多JS
      

  7.   

    <script type="text/javascript" language="javascript>
    function CreateDiv()
    {
        var div=document.createElement("<div id='div1' style='position:absolute;visibility:visible;background:#ffffff;filter:alpha(opacity=65);z-index:2;left:0;top:0;width:100%;height:100%;' onselectstart='return false' oncontextmenu='window.event.returnvalue=false'></div>");
        document.body.appendChild(div);
        document.getElementById('div1').innerHTML="<Table style='width:100%;height:100%;' oncontextmenu=return(false)><tr><td  align='center' valign='middle'><img src='IMG/load.gif'></img><br>Loading data...</td></tr></Table>";
    }
    </script>
    <script type="text/javascript" language="javascript" event="onclick" for="ctl00_ContentPlaceHolder1_btnEnquire">
     CreateDiv();
      </script>上面是我寫的代碼,但是我加一個Button,就多寫一段JS
    這樣很不好,但是發現把這個JS打包到控件里,會遇到問題,而且拖兩個Button就實現不了,所以才想看下能不能不用JS完成
      

  8.   

    我这边已经实现了,刚刚帖的是部分代码,做成控件也一样,只要把那段代码复制到button的输出事件里,给button加上image属性,将image传递给run("");这个控件应该比较好写.难点是判断button执行时间,这个也可以用js实现,用setInterval('count()',2000); 就可以了.但是我不建议写成控件,这样不灵活.因为我的项目是多语言的,我写了一个遍厉也面上的所有控件的类,用来实现多语言版本,再这里判断,如果是button,在加上onmouseup=run();事件if (control is Button)
    {
    if((control as Button).CommandName=="yes")
    {
    string strpath = "";
    if(filePath!="")
    {
    string[] path = filePath.Split('.');
    for(int k=0;k<path.Length;k++)
    {
    strpath+="../";
    }
    }
    strpath+="images/ajax_loading.gif";
    (control as Button).Attributes.Add("onmouseup","run('"+strpath+"');");
    }
    (control as Button).Text=LanguageSwitch((control as Button).ID,strFileName,filePath);
    }
      

  9.   

    可以参考Discuz!NT源代码,里面有这个功能.
    http://www.discuznt.com/
      

  10.   

     <script language="javascript">
               var popupObj = window.createPopup();
        var popupBodyObj = popupObj.document.body;
           popupBodyObj.style.border = "1px #888 ridge";
           popupBodyObj.style.cursor = "hand";
        popupBodyObj.style.padding="1px 0px 1px 2px";
        popupBodyObj.innerHTML ="<TABLE width=100% height=100%><TR align=center height=60%><TD valign=bottom><img src='../image/pc.gif' border=0>"
        +"</TD></tr><tr align=center><TD valign=top>"
            +"<span style='font-family:宋体;font-size:14px;color:#006699;'>正在处理数据……</span></TD></TR></TABLE>";        function showpop()
        {
        popupObj.show(0, 0,document.body.offsetWidth,document.body.offsetHeight,document.body);
        }
        function hidepop()
        {
        popupObj.hide();
        }
        var ti=window.setInterval("changeState()",200);
        function changeState()
        {
        _state=document.readyState;
        if (_state == "loading")
        showpop();
        if (_state == "complete")
        hidepop();
        }
             </script>
    将changeState()函数加到<body onreadystatechange="changeState();">里面,
    然后页面的任何执行事件,当没有执行完时出现的都是进度条图片,其中pc.gif是一个自己制作的进度条图片,就像登陆QQ时出现的那样的进度条
    ,也可以从网上下载啊。哈哈
      

  11.   

    yfqvip
    -------------------------
    我的都是只有c#的,所以比較好解決.
    但是如果說要判斷時間的話是不顯示的
    因為有些地方是要連SAP,數據多了 就會很慢
    沒有辦法預計時間的,但是這段 JS可以解決
    <script   type="text/javascript"   language="javascript"   event="onclick"   for="ctl00_ContentPlaceHolder1_btnEnquire"> 
      CreateDiv(); 
        </script>
    但是我想看下直接把圖片等東西都打包到DLL里好不好實現這效果.
      

  12.   

    如果要判断事件执行的时间,写成控件不太容易.反之,就比较容易写,只要把提示窗口的刷新频率做为控件的属性来设置就行了.
    图片打包到DLL不知道是怎么做的.
      

  13.   

    因為寫成控件比較難,所以放棄了
    現在寫了個JS文件,可是發現
    有時候後會報錯 Internet Explorer cannot open the internet site '''''''操作已終止
    問題應該是出在下面紅色部分,但是不知道怎樣改善.
    請幫忙,謝謝
    function createDiv()
    {
        var   url   =  window.location.pathname;
        var arr=url.split("/");
        var addr="";
        for (var i=3;i<arr.length;i++)
        {
            addr += "../";
        }
        var div=document.createElement("<div id='div_show_d' style='position:absolute;visibility:visible;margin:0px;background:#f5f5f5;filter:alpha(opacity=65);z-index:2;top:0;width:100%;height:100%;' onselectstart='return false' oncontextmenu='window.event.returnvalue=false'></div>");
        document.body.appendChild(div);
        document.getElementById('div_show_d').innerHTML="<Table style='width:100%;height:100%;' oncontextmenu=return(false)><tr><td  align='center' valign='middle'><img src='" + addr + "IMG/load.gif' /><br/><font style='font-size:18pt'>Loading data...</font></td></tr></Table>";
        document.getElementById('div_show_d').style.display="none";

        //DIV背景色修改background:#f5f5f5;
        //DIV不透明度修改filter:alpha(opacity=65);
        //DIV圖片鏈接地址修改<img src='IMG/load.gif' />
        //DIV提示文字修改<font style='font-size:18pt'>Loading data...</font>
    }
    function showDiv() 

        document.getElementById('div_show_d').style.display="block";
    }
    function hideDiv() 
    {
        document.getElementById('div_show_d').style.display="none"; 
    }
    var ti=window.setInterval("changeState()",200);
    function changeState()
    {
        if(document.body!=null)
        {
            if(document.getElementById('div_show_d')==null)
            {
                createDiv();
            }
            _state=document.readyState;
            if(_state=="loading") 
                showDiv();
            if(_state=="complete") 
                hideDiv();
        }
    }
    window.onreadystatechange=changeState();
      

  14.   

    其实都是一开始在页面写,写写的话总结出共同点,写成一个用户控件
    在其他页面引用的时候可以 page_load {
        页面需要ajax的button.attribute.add()//在此传参数找到用户控件。
     }
      

  15.   

    經過多次測試,發現寫成控件並不是一件容易的事
    但是用JS完成,還是沒問題的,提供下面代碼大家參考
    function createDiv()
    {
        var   url   =  window.location.pathname;
        var arr=url.split("/");
        var addr="";
        for (var i=3;i<arr.length;i++)
        {
            addr += "../";
        }
        var div=document.createElement("<div id='div_show_d' style='position:absolute;visibility:visible;margin:0px;background:#f3f3f3;filter:alpha(opacity=65);z-index:100;top:0;width:100%;height:100%;' onselectstart='return false' oncontextmenu='window.event.returnvalue=false'></div>");
        document.body.appendChild(div);
        document.getElementById('div_show_d').innerHTML="<Table id='td_show_d' style='width:100%;height:100%;' oncontextmenu=return(false)><tr><td align='center' valign='middle'><img src='" + addr + "IMG/load.gif' /><br/><font style='font-size:18pt'>Loading data...</font></td></tr></Table>";
        document.getElementById('div_show_d').style.display="none";    //DIV背景色修改background:#f5f5f5;
        //DIV不透明度修改filter:alpha(opacity=65);
        //DIV圖片鏈接地址修改<img src='IMG/load.gif' />
        //DIV提示文字修改<font style='font-size:18pt'>Loading data...</font>
    }
    function showDiv() 

        document.getElementById('div_show_d').style.display="block";
        var iframe=document.createElement("<iframe id='iframe_show_d' style='position:absolute;visibility:visible;margin:0px;filter:alpha(opacity=0);z-index:10;top:0;width:100%;height:100%;' onselectstart='return false' oncontextmenu='window.event.returnvalue=false'></iframe>");
        document.body.appendChild(iframe);
    }
    function hideDiv() 
    {
        document.getElementById('div_show_d').style.display="none";
        if(document.getElementById('iframe_show_d')!=null)
        {
            var iframe=document.getElementById('iframe_show_d');
            iframe.removeNode(true);
        }
    }
    var ti=window.setInterval("changeState()",100);
    function changeState()
    {
        if(document.body!=null)
        {
            _state=document.readyState;
            if(_state=="loading")
            {
                if(document.getElementById('div_show_d')==null)
                {
                    changeState();
                    return;
                }
                showDiv();
            }
            if(_state=="complete") 
            {
                if(document.getElementById('div_show_d')==null)
                {
                    createDiv();
                }
                hideDiv();
            }
        }
    }
    window.onreadystatechange=changeState();