首页 新闻 会员 周边

asp.net 用ajax到后台取值赋给html控件,可是取到的值一直没变?

1
[已解决问题] 解决于 2012-06-13 18:27
页面js代码:
<script src="js/jquery-1.7.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        var timer;
        function progressShow()
        {
            //$("#progressSpan").text("<%=progressInt %>%");
            $.ajax({
                //url: "自写progressBar.aspx/returnProgress",
                //type: "text/plain",
                //contentType: "application/text;charset=utf-8",
                url: "ashx/returnProgress.ashx",
                success: function(data){
                    alert(data);
                    $("#progressDiv").width(data);
                    $("#progressSpan").text(data);
                },
                error: function(error){
                    alert("error: "+error);
                }
            });
        }
        window.onload = function(){ timer = setInterval(progressShow,1500);}
    </script>

 

body:
<div id="contentDiv" style="width:100px; background-color:Gray;">
        <div id="progressDiv" style="width:1px; background-color:Red;">
            <span id="progressSpan" style="color:Black;"></span>
        </div>
    </div>

 

aspx.cs:

namespace progressBar进度条
{
    public partial class 自写progressBar : System.Web.UI.Page
    {
        public static int progressInt;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Thread thread = new Thread(new ThreadStart(progressUp));
                thread.Start();
            }
        }

        public void progressUp()
        {
            for (int i = 1; i < 101; i++)
            {
                Thread.Sleep(2000);
                progressInt = i;
            }
        }


        [WebMethod]
        public static string returnProgress()
        {
            //HttpContext.Current.Response.Write(progressInt);
            //HttpContext.Current.Response.End();
            return progressInt.ToString();
        }

    }
}

ashx:

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class returnProgress : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write(自写progressBar.progressInt);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

实际上progressInt一直在增加,可是为什么取出来的progressInt全是0?


AaronLi的主页 AaronLi | 初学一级 | 园豆:41
提问于:2012-06-13 14:39
< >
分享
最佳答案
0

url: "ashx/returnProgress.ashx"+new Date(),

试试

奖励园豆:5
心未鳴 | 菜鸟二级 |园豆:223 | 2012-06-13 17:26

自写progressBar的Page_Load没执行过 增加数字的线程没启动

心未鳴 | 园豆:223 (菜鸟二级) | 2012-06-13 17:30

@心未鳴: 有启动了线程吧,用个timer把数字显示出来确实是在自增啊

AaronLi | 园豆:41 (初学一级) | 2012-06-13 18:22

 url: "ashx/returnProgress.ashx?"+new Date(),

原来这样就可以了

AaronLi | 园豆:41 (初学一级) | 2012-06-13 18:28
其他回答(2)
0

你确认Page_Load方法被调用了吗?

dudu | 园豆:30994 (高人七级) | 2012-06-13 15:32

确定,我用timer定时取过 自写progressBar.progressInt的值,确实在自增啊

支持(0) 反对(0) AaronLi | 园豆:41 (初学一级) | 2012-06-13 15:36
0

单步调试下看看

Jaryleely | 园豆:367 (菜鸟二级) | 2012-06-13 16:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册