Friday, January 6, 2012

How to use PageMethods in .net

PageMethods Example

Lets look at a simple example of how to use PageMethods.

The things you need to do in your code behind is add
  • using System.Web.Script.Services;
If you want to call WebService then you have to use

  • using System.Web.Services;

In .Cs(Code behind) file write the code

[ScriptMethod, WebMethod]
    public static string callLogoff()
    {
        // to do your code 
        return "what you want"
    } 

In ASPX Page

  • Add scriptmanager this within form method and make
     
    EnablePageMethods="true"
  • Now write script method within head tag

       function closesession() {
           if (window.event.clientY < 0) {
               PageMethods.callLogoff();
               alert("Thank you for Visiting. Logging out now.....")
           }
       }
  

  • In body tag add 
onbeforeunload="closesession()"



---------------------------------------------

Hope this will help you.