title: Calling a Web Method from ASPX Code Behind
body: <p>I'm trying to figure out a way that I can call a Web Method (located as a public static method in the code behind of an aspx page) from another ASPX Code Behind page. </p>
  
  <p>This is the code behind of Page A.aspx</p>
  
  <pre><code>public partial class PageA : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
      // other code not relevant is here  
      }
  
      [WebMethod(true)]
      public static string GetStringInfo()
      {
           // do some stuff here to build my string
           // return string
      }
  }
  </code></pre>
  
  <p>On page B, I need to be able to call GetStringInfo() during page load or some other event to get the information. the GetStringInfo() is fairly complex and for reasons outside of my control, can't be moved elsewhere or rebuilt presently.</p>
  
  <p>How can I consume the web method above from another page's code behind?</p>
  
  <p>I've tried instantiating a copy of the other page (PageB), such as:</p>
  
  <pre><code>public partial class PageB : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
         PageA page = new PageA();
         page.GetStringInfo();        
      }
  }
  </code></pre>
  
  <p>The problem here is since it's dynamically compiled, I don't have an easy namespace to reference and access. I've tried adding one, and it ignores it.</p>
  
  <p>This project is on .net 3.5, C#, and is a web site project (not a web application).</p>
  
  <p>Any help is appreciated!</p>