Ajax call to webmethod from custom control

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

Ajax call to webmethod from custom control

All Replies

Posted by Community Admin on 09-Dec-2013 00:00

I have created a custom control for sitefinity 6.1 that I need to call a webmethod in my code behind using ajax.  Does anyone have any idea how to accomplish this? 

I tie my vb code behind to the ascx this way.
Protected Overrides ReadOnly Property LayoutTemplateName() As String

Get
Return "SotirISQuizModule.SotirISQuizModule.ascx"
End Get

End Property

and my webmethod in the vb code behind thus:
<WebMethod()> _
Public Shared Function LectureStarted() As String
  'Code to create a db record for the time the lecture was started
End Function

and my ajax call in my ascx thus:
$.ajax(
method: "POST",
//url: "~/SotirISQuizModule.SotirISQuizModule.ascx/LectureStarted", - Forbidden
//url: "~/SotirISQuizModule.SotirISQuizModule/LectureStarted", - Not Found
//url: "/SotirISQuizModule.SotirISQuizModule.ascx/LectureStarted", - Forbidden
//url: "/SotirISQuizModule.SotirISQuizModule/LectureStarted", - Not Found
//url: "/SotirISQuizModule.ascx/LectureStarted", - Forbidden 
//url: "SotirISQuizModule.SotirISQuizModule.ascx/LectureStarted", - Forbidden
//url: "SotirISQuizModule.ascx/LectureStarted", - Forbidden
//url: "SotirISQuizModule/LectureStarted", - Not Found
//url: "/LectureStarted", - Not Found
 url: "/SotirISQuizModule/LectureStarted",  //- Not Found
success: function ()
alert('Success');
,
error: function (XMLHttpRequest, textStatus, errorThrown)
alert("Status: " + textStatus + " " + "Error: " + errorThrown);

);

At the end of each url: is the result of the ajax call

Any help or ideas would be great.
If you have a sample in C# I can convert it.

Thank You

Posted by Community Admin on 10-Dec-2013 00:00

This issue was resolved using a RadAjaxManager to call the code behind.

Posted by Community Admin on 16-Jan-2014 00:00

Can you explain how?

Posted by Community Admin on 16-Jan-2014 00:00

I ended up not using a WebMethod instead I did the following:

In my .ascx file I added a RadAjaxManager the following: 
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

 <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" onAjaxRequest="RadAjaxManager1_AjaxRequest"></telerik:RadAjaxManager>

In a javascript function on the page that is called when the necessary event occurs I added the code:

var ajaxManager = $find("<%= RadAjaxManager1.ClientID %>");
ajaxManager.ajaxRequest("LECTUREEND");

In my code behind (which is in VB) I added:
 
        Protected ReadOnly Property RadAjaxManager1() As RadAjaxManager           
            Get
                Return MyBase.Container.GetControl(Of RadAjaxManager)("RadAjaxManager1", True)
            End Get       
         End Property

In the InitializeControls method in my code behind I added: 

AddHandler RadAjaxManager1.AjaxRequest, AddressOf Me.RadAjaxManager1_AjaxRequest

And also in the code behind I added:       
Private Sub RadAjaxManager1_AjaxRequest(sender As Object, e As AjaxRequestEventArgs)           
'Do Processing Here       
End Sub

I hope this helps

Posted by Community Admin on 16-Jan-2014 00:00

Hi Bill,

Thanks for your prompt reply.

I actually got around this in the end by creating a separate .aspx page in the root of the project, and putting the WebMethod in the code behind of that page. This then enabled me to then make the standard ajax call through jQuery as usual, from my widget (ascx) embedded in the Sitefinity "Page"... 

ContactForm.ascx

$.ajax(
type: "POST",
    url: "MessageRelay.aspx/SendMessage",
    data: jsonData,
    contentType: "application/json",
    dataType: "json",
    success:
        function (response)
            //hide all elements
            $("#contactForm").addClass("hide");
            //show success message
            $("#successPanel").removeClass("hide");
        ,
    error:
        function (msg)
            alert('Sorry, there was an error, please try again later');
        
);


MessageRelay.aspx.cs
[WebMethod]
public static bool SendMessage(int contactType, string first, string last, string number, string email, string msg, int savings, int products, int services, string callTime)
    //code here....

I've burned through hours today trying to get this working, I'm not entirely happy with the result (I would prefer the web method in the code behind of the widget, moreover I don't really like the random .aspx dumped into the root). It will suffice for now..

Thanks again. 

Posted by Community Admin on 16-Jan-2014 00:00

I spent a lot of time on it too.  Glad you got it working and thanks for sharing your solution.

This thread is closed