<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(); %>
};
当我关闭浏览器中的选项卡/页面时,上面的JS文件会运行。但是当程序开始运行时,我调用的函数<%PageO(); %>
会执行。只有在关闭选项卡/页面而不是在程序开始时才能执行此函数调用后面的代码(C#)。
此代码中有任何错误吗?
答案 0 :(得分:1)
试一下 - 您必须使用 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>
ScriptManager
,如下所示 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
在.aspx.cs文件中使用下面的WebMethod文件
`
[System.Web.Services.WebMethod]
public static string PageO()
{
return "Yes this is working";
}
`