+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Ajax call to webmethod from custom control

Ajax call to webmethod from custom control

6 posts, 1 answered
  1. Bill
    Bill avatar
    31 posts
    Registered:
    08 Apr 2013
    09 Dec 2013
    Link to this post
    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
  2. Bill
    Bill avatar
    31 posts
    Registered:
    08 Apr 2013
    10 Dec 2013
    Link to this post
    This issue was resolved using a RadAjaxManager to call the code behind.
    Answered
  3. Mic Bravo Alpha
    Mic Bravo Alpha avatar
    5 posts
    Registered:
    13 Sep 2013
    16 Jan 2014 in reply to Bill
    Link to this post
    Can you explain how?
  4. Bill
    Bill avatar
    31 posts
    Registered:
    08 Apr 2013
    16 Jan 2014 in reply to Mic Bravo Alpha
    Link to this post
    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
  5. Mic Bravo Alpha
    Mic Bravo Alpha avatar
    5 posts
    Registered:
    13 Sep 2013
    16 Jan 2014 in reply to Bill
    Link to this post
    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. 
  6. Bill
    Bill avatar
    31 posts
    Registered:
    08 Apr 2013
    16 Jan 2014 in reply to Mic Bravo Alpha
    Link to this post
    I spent a lot of time on it too.  Glad you got it working and thanks for sharing your solution.
6 posts, 1 answered