如何在特定时间从javascript调用C#中的函数
How to call function in C# from javascript at particular time
<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#( 中的此函数调用应仅在选项卡/页面关闭时执行,而不是在程序开始时执行。
此代码中有什么错误吗?
试试这个 - 你必须使用 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>
- 您必须在.aspx页面中添加
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"; }
'
相关文章:
- 如何从模块链中调用函数.导出到节点中
- 调用函数内部的函数
- 在javascript中调用c函数
- DropDownListFor赢得't在更改时调用函数
- Javascript页面调用函数
- 在动画结束之前调用函数
- 允许父窗口在其不同域的子iframe上调用函数
- 运行Infinite Scroll后调用函数时出现问题
- JavaScript:在调用函数的文本输入上按enter键
- 使用大括号和不使用bracs调用函数的区别
- javascript在事件上调用函数
- 从index.html调用函数,该函数无限循环
- 从带参数的字符串变量调用函数中的函数
- 为什么 JS 不在滚动时调用函数
- 是否可以在不更改上下文的情况下调用函数.apply
- 如何在ES6类中使用参数调用函数
- AngularJS:调用函数时编号不更新
- JavaScript中的立即调用函数表达式(IIFE)-传递jQuery
- 在JavaScript中调用函数时自定义此选项
- 调用函数中声明的变量