Question

I am trying to call a LoadFile() function every X seconds using setInterval, and I want to call this in my document ready function , and a scriptmanager on my aspx page sets EnablePageMethods as true. But this doesnt seem to work and returns PageMethod not defined error. My code is given below


<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)
        {

        }
    }

I am relatively new to Ajax, any help and insight is much appreciated.

Thanks,

Was it helpful?

Solution

Your method should be declared as static in order to be used with PageMethods.

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

Best regards

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top