Calling ServerSide Method using JS (PageMethod)

1. ASPX Page
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript" >
    function callMe()
    {
        PageMethods.sayhello(document.getElementById('text1').value,oncomplete);
    }
    function oncomplete(val)
    {
        alert(val);
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" >
    </asp:ScriptManager>
    <div>
        <asp:TextBox ID="text1" runat="server"></asp:TextBox>
        <input id="Button1" type="button" value="button" onclick="javascript:callMe();" /></div>
    </form>
</body>
</html>


2. Code Behind
Imports System.Web.Services
Imports System.Web.Script.Services
Partial Public Class PageAjax
    Inherits System.Web.UI.Page

    <System.Web.Services.WebMethod()> _
    <System.Web.Script.Services.ScriptMethod()> _
    Public Shared Function sayhello(ByVal str As String) As String
        Return "Hello " & str
    End Function
End Class

No comments:

Post a Comment