call a function of code-behind (.vb or .cs) file in JavaScript

Following code shows how you can use a function of a code-behind file in JavaScript (.js) file. In this example, you will see that when a loged-in user closes his browser Window (instead of loging-out), how you can log him out programatically by accessing the FunctionToLogOut() function of .vb file in a JavaScript file.  


contents of “MyScript.js” (JavaScript) file_


function myunload()

{

 if (window.event.clientX < 0 && window.event.clientY < 0)

 {

   alert("You are about to log out...");

   PageMethods.FunctionToLogOut();

 }

}


***************************************************************************************

In the <body> tag of your web page_


<body onunload="myunload()">


***************************************************************************************

In the code behind_


<WebMethod()>_

Public Shared Function FunctionToLogOut()


[ Yor code for Loging-Out user will go here... ]


End Function


***************************************************************************************

In the <form> tag of web page_


<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="True" EnablePageMethods="True">

  <Scripts>

        <asp:ScriptReference Path="MyScript.js"/>

  </Scripts>

 </asp:ScriptManager>

 

Comments

Popular posts from this blog

GROUP BY, CUBE, ROLLUP and SQL SERVER 2005

How to get content of Ckeditor & Fckeditor using Javascript

How to Fix Error- Sys.WebForms.PageRequestManagerTimeoutException - The server request timed out