Search code examples
c#code-behinddelayed-execution

Stop C# Code From Executing Every Time Page Loads


I have a public string in my code behind file that I intend to call from javascript when I'm ready for it. However, since it's not really tied to a particular event, it just fires every time the page loads. I would set it to only fire if(IsPostBack) but the control I'm using (for many other reasons) is an input type=button so the page never really does postback meaning the code will never fire.

How do I have something like:

public string dontExecuteYet()
 {
  Do some server side stuff
  Then redirect on delay
  return string
  }

in my Code Behind such that it doesn't run until I tell it to?


Solution

  • to me this is ASP.Net Ajax + WebMethod.

    see for exemple :

    1. http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.aspx#Y1843
    2. http://www.xdevsoftware.com/blog/post/Call-WebMethod-from-Javascript-in-ASPNET.aspx
    3. ASP.NET 4 jquery ajax webmethod call )