I got this source code from internet.
This source purpose is to call ASP.Net function or method from JavaScript.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default5.aspx.vb" Inherits="Default5" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Developed by Awais Hafeez</title>
</head>
<body>
<script language="javascript" type="text/javascript">
    function CalcSum(Src)
    {
        if (document.getElementById("<%= txtFirstNo.ClientID %>").value != '' && document.getElementById("<%= txtSecNo.ClientID %>").value != '')
        {
            PageMethods.getSum(document.getElementById("<%= txtFirstNo.ClientID %>").value, document.getElementById("<%= txtSecNo.ClientID %>").value, FillSumBox);
        }
    }
    function FillSumBox(val)
    {
        document.getElementById("<%= txtSum.ClientID %>").value = val;
    }
</script>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" EnablePageMethods="true">
        </asp:ScriptManager>
    <div> 
        <table align="center" bgcolor="#FFFFCC" width="50%">
            <tr>
                <td align="center" colspan="4">
                    <b>Calling Web Methods from Client-Side</b></td>
            </tr>
            <tr>
                <td align="right">
                    </td>
                <td width="5">
                    </td>
                <td>
                    </td>
                <td>
                   </td>
            </tr>
            <tr>
                <td align="right">
                    <asp:Label ID="Label1" runat="server" Text="First Number :"></asp:Label>
                </td>
                <td>
                 </td>
                <td>
                    <asp:TextBox ID="txtFirstNo" onchange="CalcSum(this)" runat="server" MaxLength="4"></asp:TextBox>
                </td>
                <td>
                    </td>
            </tr>
            <tr>
                <td align="right">
                    <asp:Label ID="Label2" runat="server" Text="Second Number :"></asp:Label>
                </td>
                <td>
             </td>
                <td>
                    <asp:TextBox ID="txtSecNo" onchange="CalcSum(this)" runat="server" MaxLength="4"></asp:TextBox>
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <asp:Label ID="Label3" runat="server" Text="Sum of Numbers :"></asp:Label>
                </td>
                <td>
                    </td>
                <td>
                    <asp:TextBox ID="txtSum" runat="server" ReadOnly="True"></asp:TextBox>
                </td>
                <td>
             </td>
            </tr>
        </table>    
    </div>
    </form>
</body>
</html>
Imports System.Web
Imports System.Web.UI 
Imports System.Web.Services

Partial Class Default5
    Inherits System.Web.UI.Page

    <System.Web.Services.WebMethod()> _
    Public Function GetSum(ByVal num1 As Integer, ByVal num2 As Integer)
        Return (num1 + num2)
    End Function
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
End Class

In JavaScript error listing, appeared like this : 'PageMethods is underfined.'

Thanks.

Dear,
NEVER NEVER NEVER GIVE UP
Keep it up.

PageMethods must be Public Shared.

Change Public Function GetSum() to Public Shared Function GetSum().

PageMethods must be Public Shared.

Change Public Function GetSum() to Public Shared Function GetSum().

I have changed it, but the error is still same....

I have changed PageMethods.getSum to PageMethods.GetSum in the CalcSum javascript function.

It is working for me.

Ehmm.....still could not solve my problem...

I don't get at where my mistake is :'( :( :-/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.