[英]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(); %>
的函數<%PageO(); %>
當程序開始自行運行時,將執行<%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>
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"; }
`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.