页面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?
url: "ashx/returnProgress.ashx"+new Date(),
试试
自写progressBar的Page_Load没执行过 增加数字的线程没启动
@心未鳴: 有启动了线程吧,用个timer把数字显示出来确实是在自增啊
url: "ashx/returnProgress.ashx?"+new Date(),
原来这样就可以了
你确认Page_Load方法被调用了吗?
确定,我用timer定时取过 自写progressBar.progressInt的值,确实在自增啊
单步调试下看看