0
<script type="text/javascript">
   var startTime = new Date();
   var TimeTaken;
        //Start the clock!
   window.onbeforeunload = function ()        
   {
       var endTime = new Date();
       //Get the current time.
       var timeSpent = (endTime - startTime);
       seconds = (timeSpent / 1000) % 60;
       TimeTaken = parseInt(seconds);
       //return TimeTaken;
       var temp = document.getElementById('<%=Label1.ClientID %>').value;
       temp = TimeTaken;
       alert(temp);
         <%PageO(); %>
  };

The above JS file runs when I close the tab/page in browser. But the function Which I called <%PageO(); %> executes when the programs starts running itself. This function call in code behind(C#) should execute only when the tab/page is closed not at the start of the program.

Any mistakes in this code?

SRIRAM RAMACHANDRAN
  • 297
  • 3
  • 8
  • 23

1 Answers1

1

Try This - You have to use PageMethods

<script type="text/javascript">
var startTime = new Date();
   var TimeTaken;
        //Start the clock!
   window.onbeforeunload = function ()        
   {
       var endTime = new Date();
       //Get the current time.
       var timeSpent = (endTime - startTime);
       seconds = (timeSpent / 1000) % 60;
       TimeTaken = parseInt(seconds);
       //return TimeTaken;
       var temp = document.getElementById('<%=Label1.ClientID %>').value;
       temp = TimeTaken;
       alert(temp);
       // USE PAGEMETHODS FOR CODE BEHIND METHOD CALL
         PageMethods.PageO(function (response) {                 
        alert(response);
    });
  };
</script>
  • And you have to add ScriptManager in your .aspx page as below

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

  • And in .aspx.cs file your Method with WebMethod as below

    `

    [System.Web.Services.WebMethod]

    public static string PageO()
         {
            return "Yes this is working";
         }
    

    `

prog1011
  • 3,425
  • 3
  • 30
  • 57
  • @SRIRAMRAMACHANDRAN - Use PageMethod as above code example. – prog1011 Feb 27 '15 at 06:55
  • yeah thanks ... the function is called when I close the tab ... but I need to pass the temp value to C# function also ... so only I just moved the value to Label1.. But if i use public static ... I cannot access the controls .. – SRIRAM RAMACHANDRAN Feb 27 '15 at 07:02
  • I would not recommend using asp.net ajax (formerly known as atlas) / webmethods, because these are outdated technologies, and while they are still supported it's a dead-end, especially given that better alternatives are readily available. – Andrew Savinykh Feb 27 '15 at 07:03
  • I'm not saying it didn't. I'm saying you are better off not using it, because it is not a good solution. – Andrew Savinykh Feb 27 '15 at 07:06
  • @zespri - the alternate option for PageMethod is `ajax call` - you can use `ajax call` also. – prog1011 Feb 27 '15 at 07:07
  • @SRIRAMRAMACHANDRAN - if you want to pass `label` value to your static method. You can paas that value like `PageMethods.PageO('label value')` and change your c# function like `public static string PageO(string value){}` – prog1011 Feb 27 '15 at 07:08