Using AJAX methods from a widget (UserControl)

Posted by Community Admin on 04-Aug-2018 18:07

Using AJAX methods from a widget (UserControl)

All Replies

Posted by Community Admin on 25-Oct-2011 00:00

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.

Posted by Community Admin on 26-Oct-2011 00:00

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.

Posted by Community Admin on 28-Oct-2011 00:00

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

This thread is closed