.net 利用ICallbackEventHander做进度条,无刷新!!!

功能:

   将后台的add()函数中的i变量值返回到前台,以控制进度条长度。

 

前台html:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProgressBar.aspx.cs" Inherits="ProgressBar" %>

<!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" >
<head runat="server">
    <title>无标题页</title>
   <script>
   function SendData(){
   //arg=document.getElementById('TextBox1').value;
   //document.getElementById('TextBox1').value='0';
   <%=ClientScript.GetCallbackEventReference(this,"1","GetData",null) %>;
   }
   function GetData(arg){
            var ret=arg.split(',');
            document.getElementById('test').style.width=parseInt(ret[0])*10+"px";
             document.getElementById('len').innerHTML=ret[0]+"%";
            if(ret[1]=='NO')
                setTimeout("SendData()",500);
           }
   </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div id="test" style="height:20px; background-color:Blue; width:0px;"></div><a id="len"></a><input id="Button1" type="button" value="button" οnclick="SendData()" /><br/>
       
    </div>
    </form>
</body>
</html>

 

 

后台cs:

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;

public partial class ProgressBar : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler//继承接口
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            th = null;
            RetValue = 0;
        }
    }
    private static Thread th;
    private static  int RetValue;
    void ICallbackEventHandler.RaiseCallbackEvent(string Url)//实现方法
    {
        if (th == null)
        {
            th = new Thread(add);
            //th.TrySetApartmentState(ApartmentState.STA);
            th.Start();
        }
    }
    string ICallbackEventHandler.GetCallbackResult()//实现方法
    {

         //在这里可以调用自己的方法,处理自己的过程
        if (RetValue == 100)
            return RetValue.ToString() + ",OK";
        return RetValue.ToString() + ",NO";
    }
    void add()//控制RetValue值
    {
        for (int i=0; i <= 100;i++ )
        {
            RetValue=i;
            Thread.Sleep(500);
        }
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值