Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Troubles with calling a PageMethod from javascript [solved]

Troubles with calling a PageMethod from javascript [solved]

Scheduled Pinned Locked Moved ASP.NET
javascriptdebugginghelpsysadmintools
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Calla
    wrote on last edited by
    #1

    I've been struggling with the same problem for a few days now without getting anywhere. In my aspx page I try to call a PageMethod from a javascript and then inserting the result of that call into a div. MyPage.aspx

    <ajaxToolkit:ToolkitScriptManager runat="server" ID="ScriptManager1" EnablePageMethods="true">
    <Scripts>
    <asp:ScriptReference Path="~/javascript/myscript.js" />
    </Scripts>
    </ajaxToolkit:ToolkitScriptManager>
    ...
    ...
    <a href="javascript:myFunction();">Click to run script</a>
    ...
    ...
    <div id='theDiv'></div>

    MyPage.aspx.cs

    [System.Web.Services.WebMethod]
    public static string ReturnTheString()
    {
    return "This is the string returned";
    }

    And finally my javascript function where everything goes wrong myscript.js

    function myFunction()
    {
    PageMethods.ReturnTheString(OnSucceeded, OnFailed);
    }

    function OnSucceeded(result)
    {
    var myDiv = document.getElementById('theDiv');
    myDiv.innerHTML = result;
    }

    function OnFailed(error)
    {
    alert(error.get_message());
    }

    The problem is that I don't get the expexted "This is the string returned" as result in my OnSucceeded js function. And when I debug my website and set up a breakpoint in ReturnTheString() the code never runs.. My result parameter is sometimes just empty and sometimes has the value of almost my whole page (starting with

    C 1 Reply Last reply
    0
    • C Calla

      I've been struggling with the same problem for a few days now without getting anywhere. In my aspx page I try to call a PageMethod from a javascript and then inserting the result of that call into a div. MyPage.aspx

      <ajaxToolkit:ToolkitScriptManager runat="server" ID="ScriptManager1" EnablePageMethods="true">
      <Scripts>
      <asp:ScriptReference Path="~/javascript/myscript.js" />
      </Scripts>
      </ajaxToolkit:ToolkitScriptManager>
      ...
      ...
      <a href="javascript:myFunction();">Click to run script</a>
      ...
      ...
      <div id='theDiv'></div>

      MyPage.aspx.cs

      [System.Web.Services.WebMethod]
      public static string ReturnTheString()
      {
      return "This is the string returned";
      }

      And finally my javascript function where everything goes wrong myscript.js

      function myFunction()
      {
      PageMethods.ReturnTheString(OnSucceeded, OnFailed);
      }

      function OnSucceeded(result)
      {
      var myDiv = document.getElementById('theDiv');
      myDiv.innerHTML = result;
      }

      function OnFailed(error)
      {
      alert(error.get_message());
      }

      The problem is that I don't get the expexted "This is the string returned" as result in my OnSucceeded js function. And when I debug my website and set up a breakpoint in ReturnTheString() the code never runs.. My result parameter is sometimes just empty and sometimes has the value of almost my whole page (starting with

      C Offline
      C Offline
      Calla
      wrote on last edited by
      #2

      Today I finally found what was the cause of this behaviour - and fixed it. It turned out I was missing the following lines in my web.config file: Hopefully this might help someone else who gets stuck with this problem :)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups