2011-08-23 70 views
3

我想建立一个每秒更新一些状态文本的asp.net(c#)页面。 现在我已经实现了一个调用另一个PageMethod的按钮,它可以重新启动某些内容并需要一些时间。问题是,当我打电话重启PageMethod的,更新PageMethod的不能只要重启方法继续更新......asp.net从Javascript调用WebMethod asyncronous

我写了一个小例子来说明我的意思:

的WebMethods在我的页面:

[WebMethod] 
    public static string Update() 
    { 
     //return "a" to see when the Update PageMethod succeeded 
     return "a"; 
    } 

    [WebMethod] 
    public static string Restart() 
    { 
     //the restart will take a while 
     Thread.Sleep(2000); 
     //return "a" to see when the Restart PageMethod succeeded 
     return "a"; 
    } 

HTML元素更新:

<p id="update" style="float:left;"></p> 
<p id="restart" style="float:right;"></p> 

的PageMethod的来电:

callUpdate() 
      function callUpdate() { 
       PageMethods.Update(function (text) { 
        //itself+text from pagemethod 
        $('#update').text($('#update').text() + text); 
       }); 
       setTimeout(callUpdate, 1000); 
      } 

      callRestart() 
      function callRestart() { 
       PageMethods.Restart(function (text) { 
        //itself+text from pagemethod 
        $('#restart').text($('#restart').text() + text); 
       }); 

       setTimeout(callRestart, 1000); 
      } 

注意:更新也被称为每一秒都完成之后,只是为了看看它是如何工作

为了澄清:我想PageMethods独立执行到其他PageMethod的完成。

我也飞过像一些链接: http://esskar.wordpress.com/2009/06/30/implementing-iasyncresult-aka-namedpipeclientstream-beginconnect/

http://msdn.microsoft.com/en-us/library/aa480516.aspx

但我不认为这是我需要什么 我真的不知道如何称呼,从(?)使用Javascript(的BeginXXX和Endxxx)

* 编辑:*

关于到的Massimiliano Peluso的,js的ç颂诗是这样的:

 callUpdate() 
     function callUpdate() { 
      $.ajax({ 
       type: "POST", 
       url: "ServicePage.aspx/Update", 
       data: "{}", 
       contentType: 
       "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (msg) { 
        $('#update').text($('#update').text() + msg.d); 
       } 
      }); 
      setTimeout(callUpdate, 1000); 
     } 

     callRestart() 
     function callRestart() { 
      $.ajax({ 
       type: "POST", 
       url: "ServicePage.aspx/Restart", 
       data: "{}", 
       contentType: 
       "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (msg) { 
        $('#restart').text($('#restart').text() + msg.d); 
       } 
      }); 
      setTimeout(callRestart, 1000); 
     } 

注:当我用新的JS运行页面时,恰好有同样的问题之前:直到重新启动方法完成的更新方法无能为力。

回答

2

这是因为在没有其他请求被处理的请求只能进行。 那是因为两个进程无法访问相同的SessionState(会话状态不是线程安全)。

所以要达到这个要求是在同一时间处理,你必须设置EnableSessionState@Page指令要么'ReadOnly''false'

3

您应该使用异步调用来调用页面方法。

看看下面。它是调用使用JQuery

$.ajax({ 
    type: "POST", 
    url: "PageName.aspx/MethodName", 
    data: "{}", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(msg) { 
    // Do something interesting here. 
    } 
}); 

您应使用此代码来替换页面方法的页面方法的通用方法调用

PageMethods.Restart(function (text)) 

PageMethods.Update(function (text)) 
+0

是'msg'一个JSON对象?我尝试了'var jsonObj = $ .parseJSON(msg);警报(jsonObj);'我得到一个警告** null ** – Sonnenhut

+0

'味精。d'已经这样做了,'d'是因为.NET Framework的xss预防。 – Sonnenhut