Introduction:

In this article we will learn how to use PageMethods in asp.net or what is PageMethods in asp.net or why use PageMethods in asp.net or Call PageMethods using ASP.Net AJAX and ScriptManager with example.

Description:

ASP.Net AJAX ScriptManager enable to you to call Server Side ASP.net methods from client side without page PostBack using PageMethods. The asp.net server side methods should be public and static and  These method should be WebMethod() attribute.


ScriptManager  Enabling PageMethods:

The first step you need to do is add a ASP.Net AJAX ScriptManager to the page and set its EnablePageMethods property to true as shown below:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>

 

Example:

 

Client Side Code:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageMethod.aspx.cs"
Inherits="AspDotNet.PageMethod" %>
 
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Content/Scripts/jquery-1.8.2.min.js"></script>
    <script type="text/javascript">
        function ShowUserID() {
       PageMethods.GetUserID(document.getElementById("<%=txtUserName.ClientID%>").value,              
                OnSuccess);
        }
        function OnSuccess(response) {
            $("#<%=txtUserID.ClientID%>").val(response);
}
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server"
                    EnablePageMethods="true"></asp:ScriptManager>
            
            <div>
                Your Name :
                <asp:TextBox ID="txtUserName" runat="server" Width="200">
             
                        </asp:TextBox>
                <input id="btnGetUserName" type="button" value="Show My UserID"
                  onclick="ShowUserID ()" />
                <br />
                <br />
                Your UserID:
                <asp:TextBox ID="txtUserID" runat="server" disabled Width="200">
                        
                        </asp:TextBox>
            </div>
        </div>
    </form>
</body>
</html>


 

Server Side Code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace AspDotNet
{
    public partial class PageMethod : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        [System.Web.Services.WebMethod]
        public static string GetUserID(string name)
        {
            return  name + "Your Id Is EMP0001245";
        }
    }
}

 

Above Example ShowUserID method makes an AJAX call to the server using ASP.Net AJAX ScriptManager PageMethods and executes the GetUserID method which accepts the username and returns a string value.

 

 

Output:


How to use PageMethods in Asp.Net

I hope this article will help to you.


0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like