2012-05-09 31 views
0

我想使用setInterval每X秒調用一次LoadFile()函數,並且我想在我的文檔就緒函數中調用此函數,並且我的aspx頁面上的scriptmanager將EnablePageMethods設置爲true。 但這似乎並沒有工作,並返回PageMethod未定義的錯誤。 我的代碼如下使用setInterval和PageMethods


<script type="text/javascript" > 
    $(document).ready(function() { 
     setInterval(function() { 
      PageMethods.LoadFile();    
     }, 1000); 
    }); 
</script> 

[WebMethod] 
    public void LoadFile() 
    { 
     Collection<string> stringcollection = new Collection<string>(); 
     divLogSummary2.InnerHtml = ""; 
     try 
     { 
      StringBuilder content = new StringBuilder(); 
      if (string.IsNullOrEmpty(logFilePath)) 
      { 
       return; 
      } 

      if (File.Exists(logFilePath)) 
      { 

       using (FileStream fs = new FileStream(logFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 
       { 
        using (StreamReader sr = new StreamReader(fs)) 
        { 
         while (!sr.EndOfStream) 
         { 
          stringcollection.Add(sr.ReadLine()); 
         } 
        }       

       } 
       for (int i = stringcollection.Count - 30 ; i < stringcollection.Count; i++) 
       { 
        divLogSummary2.InnerHtml = divLogSummary2.InnerHtml + " <BR> " +      stringcollection[i]; 
       } 

      } 
     } 
     catch (Exception) 
     { 

     } 
    } 

給我是比較新的阿賈克斯,任何幫助和洞察力是非常讚賞。

感謝,

回答

0

你的方法應該被聲明爲靜態以便與PageMethods使用。

[WebMethod] 
public static void LoadFile() 
{ 
    ..... 
} 

問候

+0

三江源的soltuion,但它對於工作流即的LoadFile方法被調用,但我不能把它聲明爲靜態的,因爲我需要更新一個div的innerHTML(這使得方法本質上是非靜態的)。 – Diya

+0

是的,這對Pagemethods是一個不便。但是,您可以在javascript中更新UI,而不是在後面的代碼中執行它。只需從你的PageMethods中返回一些數據並使用js來更新UI。 – Oscar

+0

謝謝奧斯卡,我能夠讓它工作:) – Diya