asp.net - Trying to fire .aspx C# function from javascript -
i'm working on web application , need fire c# function code behind when tree button pressed on page. found code finds node me, however, c# function doesn't seem recognized when trying call js.
<script type = "text/javascript"> function onload() { var links = document.getelementbyid("<%=navtree.clientid %>").getelementsbytagname("a"); (var = 0; < links.length; i++) { links[i].setattribute("href", "javascript:nodeclick(\"" + links[i].id + "\", \"" + links[i].getattribute("href") + "\")"); } } window.onload = onload; function nodeclick(id, attribute) { //do alert(nodelink.innerhtml + " clicked" + pagemethods.node_click()); eval(attribute); } </script>
and here's c# code:
[webmethod] public static string node_click() { return "@#$"; }
if remove pagemethods.node_click() call alert works fine, not call. noticed when typing pagemethods. function doesnt appear on pop list after period (in vs)
any ideas?
also, heres cs file:
public partial class sitemaster : masterpage { protected void page_load(object sender, eventargs e) { } [webmethod] public static string node_click() { return "@#$"; } }
you cannot call asp.net ajax page method directly javascript trying do. either need enablepagemethods="true"
on asp.net ajax scriptmanager
or use jquery's .ajax()
method invoke them.
enablepagemethods
creates javascript proxy allows pagemethods.yourpagemethodname()
syntax work.
for jquery, since asp.net ajax page methods stand-alone web service methods hosted inside of asp.net page, callable ajax request via .ajax()
function.
also of note, asp.net ajax page methods automatically json-encode return value, if any; not see javascript serialization going on in page methods themselves.
read using jquery directly call asp.net ajax page methods explanation of how using both approaches.
note: prefer jquery .ajax()
route myself, have used pagemethods
syntax in past. not bunch of javascript (the proxy built enabling page methods on scriptmanager
) in page , have grown accustomed using jquery as possible in general.
Comments
Post a Comment