Tags

You cannot call server-side code ‘directly’ from client-side code. That is because by design, the server side code executes at server side and client side code at the client. However there are some workarounds. To call serverside code from javascript, you will need to use AJAX, and the easiest way out, is to use the ASP.NET AJAX Extensions.
One option while using Microsoft ASP.NET AJAX is to call ASP.NET Web services (.asmx files) from the browser by using client script. The script can call a webservice containing server-based methods (Web methods) and invoke these methods without a postback and without refreshing the whole page. However this approach could be overkill sometimes, to perform the simplest of tasks. Moreover the logic needs to be kept in a separate .asmx file. We need something that is more ‘integrated’ with our solution.
The option we are going to use in this article involves PageMethods. A PageMethod is basically a public static method that is exposed in the code-behind of an aspx page and is callable from the client script. PageMethods are annotated with the [WebMethod] attribute. The page methods are rendered as inline JavaScript.

Troubleshooting: ‘PageMethods Is ‘Undefined’’ error

1. Try setting EnablePageMethods=”true” in the script manager tag

2. Don’t add the javascript references or code in the section. Add it to the tag.

3. Page methods needs to be static in code behind.

Code

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>PageMethod</title>
    <script type="text/jscript">
    function GetUser(){
        var oUser={
            ID : "007",
            Name : "jeff"
        };
        return oUser;
    }
    
    function CallServer(){
        var oUser = GetUser(); 
        PageMethods.GetServerUser(oUser, CallServerResult);
    }
    

    function CallServerResult(result){
        alert('Server:\n'+'ID:'+result.ID+'\nName:'+result.Name);        
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
            </asp:ScriptManager>
            <input type="button" value="執行 PageMethod" onclick="CallServer()" />

            </div>
    </form>
</body>
</html>
    <System.Web.Services.WebMethod()> _
    Public Shared Function GetServerUser(ByVal Value As TUserData) As TUserData
        Value.ID = "012"
        Value.Name = "tony"
        Return Value
    End Function

<Serializable()> _
Public Class TUserData
    Dim FID As String = String.Empty
    Dim FName As String = String.Empty

    Public Property ID() As String
        Get
            Return FID
        End Get
        Set(ByVal value As String)
            FID = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return FName
        End Get
        Set(ByVal value As String)
            FName = value
        End Set
    End Property
End Class

A good article is here

http://www.dotnetcurry.com/ShowArticle.aspx?ID=109&AspxAutoDetectCookieSupport=1

Advertisements