More in this section

Forums / Developing with Sitefinity / Using AJAX methods from a widget (UserControl)

Using AJAX methods from a widget (UserControl)

3 posts, 1 answered
  1. Thomas
    Thomas avatar
    223 posts
    Registered:
    19 Jan 2011
    25 Oct 2011
    Link to this post
    Hi,

    I want to add a bit of AJAX magic to one of my pages. Just call a server-side method from JS, and get the result.
    This turns out to be more difficult than expected.

    First, I found out I can't have AJAX methods in a UserControl (http://stackoverflow.com/questions/5818265/pagemethods-is-not-defined), as I work with UserControls (ascx) rather than Pages (aspx), since the result is supposed to be a widget added to the Sitefinity Toolbox.

    From what I read, I have to create and use a web service to be able to achieve that (I'm not too happy about this, this is unneeded complexity, but oh well).
    So, I followed the steps of this article: http://msdn.microsoft.com/en-us/magazine/cc163499.aspx

    And now, after doing everything detailed in this article, I hit the following JS error as I call my AJAX method:

    q is not a function coming from line 3345 of Telerik.Sitefinity.Resources.Scripts.MicrosoftAjax.debug.js

    This is my JS code:

    Namespace.Class.HelloWorld("test", onComplete, onFailure);
     
    function onComplete(result) {
        alert(result);
    }
     
    function onFailure(result) {
        alert("Error: " + result.get_message());
    }

    My web service class:

    using System.ComponentModel;
    using System.Web.Script.Services;
    using System.Web.Services;
     
    namespace Namespace
    {
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
        [ScriptService]
        public class Class : System.Web.Services.WebService
        {
            [WebMethod]
            public string HelloWorld()
            {
                return "Hello World";
            }
        }
    }

    And I added this to my master page:

    <asp:ScriptManager ID="scriptManager" runat="server">
        <Services>
            <asp:ServiceReference Path="~/Namespace/Class.asmx" />
        </Services>
    </asp:ScriptManager>


    What am I doing wrong?
    Is there a better way to do this?

    By the way, why is this "debug" script file added even if I build in release mode? I'm thinking it's either poorly named and isn't actually a debug file, or it shouldn't be included.

    Thanks.
  2. Thomas
    Thomas avatar
    223 posts
    Registered:
    19 Jan 2011
    26 Oct 2011
    Link to this post
    I managed to get it working.

    The problem was that I passed a param on the JS side (the "test" string), while the server-side method doesn't expect a parameter. So this works either by removing the "test" param in the JS, or by making it so the HelloWorld method expects a string parameter.

    I'd still appreciate an answer to my last question.

    Thanks.
  3. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    18 Sep 2017
    28 Oct 2011
    Link to this post
    Hi Thomas,

    The .pdb files are added only in debug. There is no Sitefinity specific setting concerning generation of debug databases. I have tested disabling debug and no .pdb was generated.
    <compilation debug="false" targetFramework="4.0">
    Enabling it afterwards generated .pdb. Have you deleted all .pdb when building in release?

    Greetings,
    Stanislav Velikov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
3 posts, 1 answered