title: Call C# server side from Javascript with parameter passing
body: <p>I know this question has been asked before but nothing so far has helped me reach a final solution.</p>
  
  <p>I have an ASP web page that has numerous buttons that perform different functions. I also have a dropdown box that contains a list of objects. When I select an item from the dropdown, the button functionality remains the same in each button, however I would like a function called on the server in my C# code that receives a couple of key variables that I can then pass to an API. If only one parameter can be passed, I could pass it as a JSON string, so this is not a restriction any possible solutions should be concerned with.</p>
  
  <p>What I am having trouble with is determining the best way to do this. I have read some possibilities in things like using AJAX calls, an OnCommand attribute inside the asp button, WebMethods and a few other more obscure possibilities. I would like to avoid the hidden field approach as it does seem pretty hacky. That being said, I am open to changing my thoughts if people really do think it is the best way.</p>
  
  <p>The relevant parts of my code are as follows:</p>
  
  <p><strong>Javascript code</strong></p>
  
  <pre><code>function testAJAX()
  {
      //The console.log was called successfully, however the ProcessData function was never hit correctly
      /*console.log("hello");     
      PageMethods.ProcessData("Hello", successAjax, failAjax);*/
  
      $.ajax({ 
          type: "POST", 
          url: "UserManagement.aspx/ProcessData", 
          data: '{data:"Hello"}', // passing the parameter 
          contentType: "application/x-www-form-urlencoded; charset=utf-8", 
          dataType: "text", 
          success: function(retValue) {
              // Do something with the return value from.Net method
              alert(retValue);
              } 
          }); 
  }
  
  function successAjax(response) {
      alert(response.get_message());
  }
  
  function failAjax(response) {
      alert(response.get_message());
  }
  </code></pre>
  
  <p><strong>C# code (debugging was attempted on data += " from the server";)</strong></p>
  
  <pre><code>[WebMethod(EnableSession = true)]
  public static string ProcessData(string data)
  {
      data += " from the server";
      return data;
  }
  </code></pre>
  
  <p><strong>Calling the Javascript code</strong></p>
  
  <pre><code>&lt;form id="form1" runat="server"&gt;
      &lt;asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server" /&gt;
      &lt;button onclick="testAJAX()"&gt;Test AJAX&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>For the life of me, I simply can not get ANY Ajax or Web Method calls hitting my breakpoint in the server side code. I have tried:</p>
  
  <ul>
  <li>Copying the AJAX from the top answer in <a href="https://stackoverflow.com/questions/18610042/call-c-sharp-method-from-javascript-with-parameter">this</a> and changing the variables so that it points to my function (url: "UserManagement.aspx/ProcessData",)</li>
  <li>Moving my script tag to be inside the body tag</li>
  <li>Adding a ScriptManager to my form</li>
  <li>Made sure I was not using a Master page as apparently that changes things</li>
  <li>Using PageMethods.ProcessData(). This seems to be the closest, as it actually did hit the success function, but didn't seem to hit my ProcessData function...</li>
  </ul>
  
  <p>I'm really at a loss as to why it is not debugging on the server, if anyone has any suggestions, that would be fantastic. I'm assuming I am missing something small, but it certainly seems to be enough to halt progress. Once I can debug, I should be good to go. Thank you all in advance.</p>