1

I'm working on a web application and I need to fire a c# function from my code behind when a tree button is pressed on the page. I found some code that finds the node for me, However, the c# function doesn't seem to be recognized when trying to call it from JS.

<script type = "text/javascript">

function OnLoad() {
   var links = document.getElementById("<%=navTree.ClientID %>").getElementsByTagName("a");
   for (var i = 0; i < links.length; i++) {
     links[i].setAttribute("href", "javascript:NodeClick(\"" + links[i].id + "\", \"" + links[i].getAttribute("href") + "\")");
   }
}

window.onload = OnLoad;

function NodeClick(id, attribute) {
  //Do Something
  alert(nodeLink.innerHTML + " clicked" + PageMethods.node_Click());         
  eval(attribute);
}

</script>

and here's my C# code:

[WebMethod]
public static string node_Click()
{
  return "@#$";
}

If I remove the PageMethods.node_Click() call from the alert then it works fine, but not with the call. I also noticed when typing PageMethods. my function doesnt appear on the pop list after the period (in VS)

Any ideas?

Also, heres my cs file:

public partial class SiteMaster : MasterPage
{
  protected void Page_Load(object sender, EventArgs e)
  {
  }

  [WebMethod]
  public static string node_Click()
  {
    return "@#$";
  }
}
4
  • What is PageMethods? Is that the name of the code behind class? Somehow I don't think it is. Commented Aug 8, 2013 at 1:07
  • It's an AJAX tool, supposed to be a way of calling static C# methods. Here's where I got it from geekzilla.co.uk/View7B75C93E-C8C9-4576-972B-2C3138DFC671.htm Commented Aug 8, 2013 at 1:10
  • What does your .aspx file look like? Does it have the ScriptManager class properly setup as defined in that link? Commented Aug 8, 2013 at 1:14
  • @WillTuttle - Please consider "cleaning up" your code before posting it. Having inconsistent formatting makes it hard to read. You are asking for help, at least be considerate and make it readable. Commented Aug 8, 2013 at 2:32

1 Answer 1

1

You cannot call an ASP.NET AJAX Page Method directly from JavaScript like you are trying to do. You either need to EnablePageMethods="true" on your ASP.NET AJAX ScriptManager or use jQuery's .ajax() method to invoke them.

EnablePageMethods creates a JavaScript proxy that allows for the PageMethods.YourPageMethodName() syntax to work.

For jQuery, since ASP.NET AJAX Page Methods are really just stand-alone web service methods hosted inside of an ASP.NET page, they are callable by an AJAX request via the .ajax() function.

Also of note, ASP.NET AJAX Page Methods automatically JSON-encode their return value, if any; so you will not see any JavaScript serialization going on in the page methods themselves.

Read Using jQuery to directly call ASP.NET AJAX page methods for an explanation of how to do it using both approaches.

Note: I prefer the jQuery .ajax() route myself, but have used the PageMethods syntax in the past. I personally do not like a bunch of extra JavaScript (the proxy built by enabling page methods on the ScriptManager) in my page and I have grown accustomed to using jQuery as much as possible in general.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.