Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Call Server Side Methods Using Page Method

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 75
    Comment on it

    We all know about the jquery Ajax which is a most popular and widely used methods to make server side calls from client side. ASP.NET provides an easier approach to make server side calls from client side using Page Methods.


    Below is the demonstration of Page Method


    First create server side method which needs to be called from client side

       [System.Web.Services.WebMethod]
        public static string Display(string firstName, string lastName)
        {
            string result = "Welcome " + firstName + " " + lastName + ".";
            return result;
        }
    

    Create function on client side to call the server side method which uses PageMethods

    <script type="text/javascript">
        function Display() {
            var firstName = document.getElementById('<%=txtFirstName.ClientID %>').value;
            var lastName = document.getElementById('<%=txtLastName.ClientID %>').value;
    
            PageMethods.Display(firstName, lastName, onSucess, onError);
            function onSucess(result) {
                alert(result);
            }
    
            function onError(result) {
                alert('Something wrong.');
            }
        }
    </script> 
    

    Include the Script Manager in aspx page and set EnablePageMethods property to true

    <form id="form1" runat="server">
        <div>
            <p>Call Server Side Methods Using Page Method</p>
    
            <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"> </asp:ScriptManager>
            First Name: <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
            <br />
            Last  Name: <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
            <br />
            <asp:Button ID="btnDisplay" runat="server" Text="Signup" OnClientClick="Display(); return false;" />
        </div>
    </form>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: