Issue with json calling method from .net page

melinalt
melinalt used Ask the Experts™
on
I am using json to retrieve the company information from the database but something is not working correctly with the registration of my class:

My page source shows the following registration:

<script type="text/javascript" src="/vos/vosnet/ajaxpro/CompanyInfo,App_Web_5phg0ezg.ashx"></script>


CompanyInfo is the name of the class in my control

However, I am getting the following error:

'CompanyInfo' is undefined

Code attached below.

Any help will be appreciated.

Thanks,

HTML
------------------------

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="CompanyInfo.ascx.vb"
    ClassName="CompanyInfo" Inherits="CompanyInfo" %>
<script type="text/javascript" src="~/JavaScript/json.js"></script>
<script type="text/javascript">

    var selectedDiv = 0;

    function getCompanyInfo() {
        selectedDiv = document.getElementById("dvCompanyInfo");
        CompanyInfo.getCompanyInformation(aJaxReturnListener);
    }

    function aJaxReturnListener(response) {
        selectedDiv.innerHTML = response.value;
    }


    $(function () {

        $('#Button1').click(saveCompanyInfo);

    });


    function saveCompanyInfo() {

        var values = {};

        $("#info :input").each(function () {
            if (this.name.search(/\[\]/) > 0) //search for [] in name
            {
                if (typeof values[this.name] != "undefined") {
                    values[this.name] = values[this.name].concat([$(this).val()])
                }
                else {
                    values[this.name] = [$(this).val()];
                }
            }
            else {
                values[this.name] = $(this).val();
            }
        }
    );

        CompanyInfo.updateField(values.toJSONString(), aJaxReturnDetailsListener);

    }

    function aJaxReturnDetailsListener(response) {
        var divEx = document.getElementById("dvQry");
        divEx.innerHTML = response.value;
    }

                    
</script>
<table width="100%">
    <tr>
        <td>
            <span><b>Company Information</b></span>
        </td>
    </tr>
    <tr>
        <td>
            <div id="dvCompanyInfo">
            </div>
        </td>
    </tr>
    <tr>
        <td>
            <div id="dvQry">
            </div>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="button" id="Button1" value="Update" onclick="saveCompanyInfo();" />
        </td>
    </tr>
</table>
<script>
    getCompanyInfo()
</script>


Code behind
-------------------------------
Imports AjaxPro
Imports Newtonsoft.Json
Imports System.Text
Imports System.Runtime.Remoting.Messaging
Imports System.Reflection
Imports System.Collections.Generic



Partial Public Class CompanyInfo
    Inherits System.Web.UI.UserControl

    Private _empid As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AjaxPro.Utility.RegisterTypeForAjax(GetType(CompanyInfo))
    End Sub
    <AjaxPro.AjaxMethod()> _
    Public Function getCompanyInformation() As String
        Dim out = String.Empty
        Dim empInfo As List(Of transferFields) = EmployerInfo.getFields(empID)
        out = EmployerInfo.renderInfo(empInfo)
        Return out
    End Function

    <AjaxPro.AjaxMethod()> _
    Public Function updateField(ByVal jsonObject As String) As String
        Dim blnFirstUpdate As Boolean = False
        Dim ajaxTransfer As List(Of transferFields) = JavaScriptConvert.DeserializeObject(jsonObject, GetType(transferFields))
        Dim out As New StringBuilder
        out.Append("update emp ")
        For x = 0 To ajaxTransfer.Count - 1
            If blnFirstUpdate Then
                out.Append("set " & ajaxTransfer.Item(x).fieldName & "='" & ajaxTransfer.Item(x).fieldValue & "' ")
                blnFirstUpdate = False
            Else
                out.Append(", " & ajaxTransfer.Item(x).fieldName & "='" & ajaxTransfer.Item(x).fieldValue & "' ")
            End If
        Next
        out.Append("where col_empid='" & Me.empID & "' ")
        Return out.ToString
    End Function

#Region "Properties"

    Public Property empID() As Integer
        Get
            Return Me._empid
        End Get
        Set(ByVal value As Integer)
            Me._empid = value
        End Set
    End Property

#End Region

End Class

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
>'CompanyInfo' is undefined

Yes, where did you declare it?

var CompanyInfo = something;

Author

Commented:
leakim971:

I am registering the class on the page load of my code behind:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AjaxPro.Utility.RegisterTypeForAjax(GetType(CompanyInfo))
    End Sub


The registration is succesful because I look at the source page and I see it there:


<script type="text/javascript" src="/vos/vosnet/ajaxpro/CompanyInfo,App_Web_seiqds3m.ashx"></script>




Author

Commented:
I noticed that, with my class, there is a file that is dynamically being registered as well.  

<script type="text/javascript" src="/vos/vosnet/ajaxpro/CompanyInfo,App_Web_seiqds3m.ashx"></script>

In this case App_Web_seiqds3m.ashx

This name changes everytime that I refresh the page.

Checking VS intellicence, I see that I can also call the RegisterTypeForAjax with a second parameter:

RegisterTypeForAjax(type as System.Type, page as system.UI.page)


I tried this:

AjaxPro.Utility.RegisterTypeForAjax(GetType(CompanyInfo), Me.Page)

But still got the error

'CompanyInfo' is undefined

And the source still shows a dynamically generated page:

<script type="text/javascript" src="/vos/vosnet/ajaxpro/CompanyInfo,App_Web_koi4dovm.ashx"></script>

Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

leakim971Multitechnician
Top Expert 2014

Commented:
Ok, so check inside this file to see if you see the declaration
Replace :
<script>
    getCompanyInfo()
</script>

Open in new window

by :
<script>
window.onload = function() {
    getCompanyInfo()
}
</script>

Open in new window

Author

Commented:
Paqge source still shows:

<script>
    getCompanyInfo()
</script>

I put an alert inside function it self and I got the pop-up

function getCompanyInfo() {
        alert("HERE");
        selectedDiv = document.getElementById("dvCompanyInfo");
        CompanyInfo.getCompanyInformation(aJaxReturnListener);
    }

So the function is being called successfully too.

It is just not finding my CompanyInfo class (which is registered)

leakim971Multitechnician
Top Expert 2014
Commented:
I think CompanyInfo is "registered" too late. After the run of getCompanyInfo.

Author

Commented:
This is my entire source:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="ctl00_html" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <script type="text/javascript" language="javascript" src="../JavaScript/jQueryLibrary/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="/vos/vosnet/ajaxpro/CompanyInfo,App_Web_5k1p5ub4.ashx"></script>
    <script type="text/javascript" src="~/JavaScript/json.js"></script>
    <script type="text/javascript">

        var selectedDiv = 0;

        function getCompanyInfo() {
            selectedDiv = document.getElementById("dvCompanyInfo");
            CompanyInfo.getCompanyInformation(aJaxReturnListener);
        }

        function aJaxReturnListener(response) {
            selectedDiv.innerHTML = response.value;
        }


        $(function () {

            $('#Button1').click(saveCompanyInfo);

        });


        function saveCompanyInfo() {

            var values = {};

            $("#info :input").each(function () {
                if (this.name.search(/\[\]/) > 0) //search for [] in name
                {
                    if (typeof values[this.name] != "undefined") {
                        values[this.name] = values[this.name].concat([$(this).val()])
                    }
                    else {
                        values[this.name] = [$(this).val()];
                    }
                }
                else {
                    values[this.name] = $(this).val();
                }
            }
    );

            CompanyInfo.updateField(values.toJSONString(), aJaxUpdateListener);

        }

        function aJaxUpdateListener(response) {
            var divEx = document.getElementById("dvQry");
            divEx.innerHTML = response.value;
        }
 
                    
    </script>
</head>
<title></title>
<body id="ctl00_bodyid">
    <form name="aspnetForm" method="post" action="employerVerification.aspx" id="aspnetForm">
    <table width="100%">
        <tr>
            <td>
                <span><b>Company Information</b></span>
            </td>
        </tr>
        <tr>
            <td>
                <div id="dvCompanyInfo">
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div id="dvQry">
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" id="Button1" value="Update" onclick="saveCompanyInfo();" />
            </td>
        </tr>
    </table>
    <script>
        getCompanyInfo()
    </script>
    </form>
</body>
</html>

Open in new window

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head>
    <script type="text/javascript" language="javascript" src="../JavaScript/jQueryLibrary/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="~/JavaScript/json.js"></script>
    <script type="text/javascript" src="/vos/vosnet/ajaxpro/CompanyInfo,App_Web_5k1p5ub4.ashx"></script>
    <script type="text/javascript">

        var selectedDiv = 0;

        function getCompanyInfo() {
            selectedDiv = document.getElementById("dvCompanyInfo");
            CompanyInfo.getCompanyInformation(aJaxReturnListener);
        }

        function aJaxReturnListener(response) {
            selectedDiv.innerHTML = response.value;
        }


        $(function () {

            $('#Button1').click(saveCompanyInfo);

        });


        function saveCompanyInfo() {

            var values = {};

            $("#info :input").each(function () {
                if (this.name.search(/\[\]/) > 0) //search for [] in name
                {
                    if (typeof values[this.name] != "undefined") {
                        values[this.name] = values[this.name].concat([$(this).val()])
                    }
                    else {
                        values[this.name] = [$(this).val()];
                    }
                }
                else {
                    values[this.name] = $(this).val();
                }
            }
    );

            CompanyInfo.updateField(values.toJSONString(), aJaxUpdateListener);

        }

        function aJaxUpdateListener(response) {
            var divEx = document.getElementById("dvQry");
            divEx.innerHTML = response.value;
        }
 
                    
    </script>
</head>
<title>
</title>
<html id="ctl00_html" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<body id="ctl00_bodyid">
    <form name="aspnetForm" method="post" action="employerVerification.aspx" id="aspnetForm">
    <table width="100%">
        <tr>
            <td>
                <span><b>Company Information</b></span>
            </td>
        </tr>
        <tr>
            <td>
                <div id="dvCompanyInfo">
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div id="dvQry">
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" id="Button1" value="Update" onclick="saveCompanyInfo();" />
            </td>
        </tr>
    </table>
    <script>
        getCompanyInfo()
    </script>
    </form>
</body>
</html>

Open in new window

leakim971Multitechnician
Top Expert 2014

Commented:
please provide a link to the page if possible...

or try my suggestion ID:37394300

Author

Commented:
Unfortunately, I won't be able to give you a link since it is inside our intranet,
I tried your suggestion but still didn't work.
I am starting to think that somehow I need to specify the namespace, however, the namespace in our app is ASP so I tried ASP.CompanyInfo and still got the error

'ASP' is is undefined



Author

Commented:
OK.. Seems like my assuption is correct.

I need to add the namespace before the classname

My problem is that it doesn't seem like the application has a namespace specified

Response.Write(GetType(CompanyInfo))
Response.End()

This returns CompanyInfo  -- > Which is correct

Response.Write(GetType(CompanyInfo).Namespace)
Response.End()
<b>Returns empty</b>

Is there any other way to execute the method using jason with no namespace?
Commented:
It was in fact an issue with the namespace and I finally figured it out.

I defined the namespace using the AjaxNamespace (added this right before my class).

<AjaxPro.AjaxNamespace("CompanyInfo")> _
Partial Class CompanyInfo
...
End Class



AND

Added the following to the web.config file


web.config

<configuration>

    <location path="ajaxpro">
        <system.webServer>
            <handlers>
                <add verb="*" path="*.ashx" name="AjaxPro"  
                     type="AjaxPro.AjaxHandlerFactory,AjaxPro.2" />
            </handlers>
        </system.webServer>
    </location>

</configuration>


It is now  working properly.

Thanks leakim971 for all your help.

Author

Commented:
I've requested that this question be closed as follows:

Accepted answer: 250 points for leakim971's comment http:/Q_27525068.html#37394486
Assisted answer: 0 points for melinalt's comment http:/Q_27525068.html#37395137

for the following reason:

leakim971 thanks for your help.

Author

Commented:
I selected incorrect solution

Author

Commented:
Thanks leakim971 for all your help.
leakim971Multitechnician
Top Expert 2014

Commented:
you're welcome! Congrats!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial