C# 实现实时3秒跳转页

   

 protected void Binddata()
    
{

        
// this.mytext.InnerHtml="三秒后页面跳转<div id=\"aa\"></div>";//也可以在页面固定一个DIV区域来显示该信息
        Response.Write("三秒后页面跳转<div id=\"aa\"></div>");//先生成一个用来显示时间倒计时的DIV

        StringBuilder sb 
= new StringBuilder();//注意添加using引用 System.Text
        sb.Append("<script langage=\"javascript\">"); //用\"转义'

        sb.Append(
"var i=4;");

        sb.Append(
"function out()");

        sb.Append(
"{");

        sb.Append(
"if(i>0){ ");

        sb.Append(
"i--;}");

        sb.Append(
"else {");

        sb.Append(
"location.href=\"login.aspx\";}");//这里的login.aspx可以随便改成你想转换的页面


        sb.Append(
"document.getElementById(\"aa\").innerHTML=i; }");

        sb.Append(
"setInterval(\"out()\",1000); "); //每隔一秒发生

        sb.Append(
"</script>");

        
this.Page.Controls.Add(new LiteralControl(sb.ToString()));

    }

 

 需要再通用一点的话,把方法参数提出来后如下,各页面调用也方便很多

 

    public static void SuccessReturnScript(System.Web.UI.Page page,string writeControlID,string returnUrl)
        {
            StringBuilder sb 
= new StringBuilder();
            sb.Append(
"<script langage=\"javascript\">"); //用\"转义'

            sb.Append(
"var i=4;");

            sb.Append(
"function out()");

            sb.Append(
"{");

            sb.Append(
"if(i>1){ ");

            sb.Append(
"i--;}");

            sb.Append(
"else {");

            sb.Append(
"location.href=\"" + returnUrl + "\";}");
            sb.Append(
"document.getElementById(\"" + writeControlID + "\").innerHTML=\"操作成功,页面<font color=red>\"+i+\"</font>秒后自动跳转\"; }");

            sb.Append(
"setInterval(\"out()\",1000); "); //每隔一秒发生

            sb.Append(
"</script>");

            page.Controls.Add(
new LiteralControl(sb.ToString()));
        }

 

另外加一个倒计时显示的js代码

为了优化,day只计算了一次。hour,minitus,second,ms则放在里层反复计算

<script type="text/javascript">

 var day=hour=minitus=second=ms=0;
 var targetDate= new Date("2010/08/07 12:00:00");
 var tmp = targetDate - new Date();
 day = Math.floor(tmp / 86400000);
function RefreshTime()
 {
  tmp = (targetDate - new Date()) % 86400000;
  hour=Math.floor(tmp/3600000);
  tmp%=3600000;
  minitus=Math.floor(tmp/60000);
  tmp%=60000;
  second=Math.floor(tmp/1000);
//  ms=tmp-1000*second;
  document.getElementById("m1").innerHTML = "距离" + targetDate + "年还有 " + day + " 天 " + hour + " 小时 " + minitus + " 分 " + second + " 秒 ";
}
setInterval(RefreshTime, 1000);
</script>

 

 


posted on 2009-03-12 10:28  Theo  阅读(1249)  评论(2编辑  收藏  举报