0

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,

Diya
  • 3
  • 2

1 Answers1

0

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

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

Best regards

Oscar
  • 13,594
  • 8
  • 47
  • 75
  • Thankyou for the soltuion,it does work with respect to the flow i.e. the LoadFile method is called but I cannot declare it as static as I need to update a div innerHtml (which makes the method inherently non-static). – Diya May 09 '12 at 12:42
  • Yes, it's an inconvenience with Pagemethods. However, you can update the UI in javascript instead of doind it in code behind. Just return some data from your PageMethods and use js to update the UI. – Oscar May 09 '12 at 12:47