1

I am trying to call a code behind function directly. At the moment I am able to call the function from the PageLoad event of my ajax.aspx.vb and I pass the response through my variable which is on the ajax.aspx <%= m_result %>. This works fine. However, I do not want to go through the PageLoad event, but to target the function directly without having to put data into the <%= m_result %>. Is it possible?

I am using DotNetNuke where all the functionality are comes from ascx controls. So, in my ascx control I have the ajax as:

<script type = "text/javascript">
    function JSFunction() {
        var xx = 1;
            $.ajax({
                type: "POST",
                url: "/top3/DesktopModules/top3all/ajax.aspx/GetTestData",
                data: { 'sPageIndex': xx},
            }).done(function (response) {
             OnSuccessTest(response);
            });
    }
    function OnSuccessTest(response) {
        document.getElementById("<%= lblTest.ClientID%>").innerHTML = response;
}
</script>

In my ajax.aspx.vb:

 Protected m_result As String = ""

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim sModuleIDReferrer As String
        If Not HttpContext.Current.Request.UrlReferrer Is Nothing Then
            strUrl = HttpContext.Current.Request.UrlReferrer.ToString

            Dim sPageIndex As Integer
            If Integer.TryParse(Request("sPageIndex"), sPageIndex) Then

                GetTestData(sPageIndex)
            End If
          End If
    End Sub

 <System.Web.Services.WebMethod()> _
 Public Shared Function GetTestData(ByVal name As Integer) As String
        m_result = "some test"
        Return m_result
    End Function

In my ajax.aspx:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="ajax.aspx.vb" Inherits="Christoc.Modules.top3all.ajax" %>
<%= m_result %>
alwaysVBNET
  • 3,150
  • 8
  • 32
  • 65
  • I think you need to declare the method you want to call as a WebMethod, and I think you need to declare it static... – David W Jun 04 '15 at 13:54
  • Like @DavidW said you should probably create a WebMethod. See this [example](http://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx). In VB static methods are called Shared – zgood Jun 04 '15 at 13:55

2 Answers2

0

Believe this is what you need --

In your aspx page, add a ScriptManager control, and set its EnablePageMethods property to True

<asp:ScriptManager runat="server" ID="MySM" runat="server" EnablePageMethods="True" />

Modify your method declaration in the codebehind as follows:

<System.Web.Services.WebMethod()> _
 Public Shared Function GetTestData(ByVal name As Integer) As String
        m_result = "some test"
        Return m_result
 End Function
David W
  • 10,062
  • 34
  • 60
  • I have tried that already but it still goes through the PageLoad and since I'm not populating the m_result in the PageLoad it returns nothing. – alwaysVBNET Jun 04 '15 at 14:01
  • Do you have a ScriptManager control on your form? If not, add one, and set EnablePageMethods=True (Just edited the ansewr to include this for completeness). – David W Jun 04 '15 at 14:10
  • Oooh one other item - in your call to the function, you call the parameter "sPageIndex" but the formal parameter name is 'name'. Think those have to match the method signature. – David W Jun 04 '15 at 14:22
  • I cannot add a ScriptManager because it's already have references to a built-in script manager. On the built-in script manager I wasn't able to set the EnablePageMethods=true – alwaysVBNET Jun 04 '15 at 14:52
  • Built-in script manager? Could you expand on that a bit? Is it in a master page or something such as that? I take it it's something other than the standard ASP ScriptManager? If so, that might be a factor in your issue. Maybe if you posted some or all of your aspx markup it might shed some light on the issue. – David W Jun 04 '15 at 14:53
  • Thanks for your reply. I will do some research and let you know – alwaysVBNET Jun 04 '15 at 15:14
0

After adding <System.Web.Services.WebMethod()> as @David says, you could try changing your JS to use PageMethods, check this answer, there you can find all you need.

Hope this help

Community
  • 1
  • 1
Enrique Zavaleta
  • 2,098
  • 3
  • 21
  • 29