title: AJAX with ASP.NET: How to call a method of a user control using AJAX?
body: <p>I have a master page that has a placeholder to generate the content for <code>Default.aspx</code>
   <code>Default.aspx</code> defines a <code>User Control</code>:</p>
  
  <pre><code>&lt;asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"&gt;
      &lt;uc:maincontent ID="maincontent" runat="server" /&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p><code>User Control</code> has the following code inside:</p>
  
  <pre><code>&lt;div class="form-group"&gt;
          &lt;button type="button" class="btn btn-primary" Text="Get Data" runat="server" data-toggle="model" data-target="#myModal" title="Provide a Year"
              ID="btnGenerate" /&gt;
  &lt;/div
  </code></pre>
  
  <p>Then, I want to call a method, defined in <code>User Control</code> code behind:</p>
  
  <pre><code>[System.Web.Services.WebMethod]
  public static string GeneratePdfs()
  {
      string test = "Test String";
      return test;
  }
  </code></pre>
  
  <p>To do that I have the following <code>jQuery</code> code:</p>
  
  <pre><code>$('#btnGenerate').click(function () {
  
      if (!ValidateInput()) {
          return false;
      }
  
      $.ajax({
          type: "POST",
          url: "????"
          .....
      });
  </code></pre>
  
  <p>I have also created a <code>Generic Handler</code> with the following code. I found this example on stackoverflow</p>
  
  <pre><code>public void ProcessRequest (HttpContext context) {
  
      using (var writer = new System.IO.StringWriter())
      {
          Page pageHolder = new Page();
          var control = (UserControl)pageHolder.LoadControl("~/controls/maincontent.ascx");
          pageHolder.Controls.Add(control);
          context.Server.Execute(pageHolder, writer, false);
          context.Response.ContentType = "text/html";
          context.Response.Write(writer.GetStringBuilder().ToString());
      }
  }
  </code></pre>
  
  <p>But this example does not explain how to call a <code>Web Method</code> defined in my <code>User Control</code>.</p>
  
  <p>What is the right way to accomplish something like that?</p>