如何在asp.net中的会话超时之前触发一个函数

时间:2017-11-20 09:33:15

标签: javascript asp.net ajax session

我想调用一种方法来跟踪签入和签出用户活动。我设置会话超时以使会话到期,但在会话超时之前,我想存储用户活动。因此,我尝试使用ajax在javascript中调用方法,以便在会话到期之前运行该方法。不幸的是,该方法没有运行。以下是我的代码!

Admin.Master.cs

     protected void Page_Load(object sender, EventArgs e)
            {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    if (!this.IsPostBack)
                    {
                        Session["Reset"] = true;
                        Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
                        SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState");
                        int timeout = (int)section.Timeout.TotalMinutes * 1000 * 60;
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true);
                    }
    }

 [WebMethod]
        public void logout_timeout() //This method i have to run
        {
}

Admin.Master

 <script type="text/javascript">
        function SessionExpireAlert(timeout)
        {
            debugger
            var seconds = timeout / 1000;
            document.getElementsByName("secondsIdle").innerHTML = seconds;
            document.getElementsByName("seconds").innerHTML = seconds;
            setInterval(function () {
                seconds--;
                document.getElementById("seconds").innerHTML = seconds;
                document.getElementById("secondsIdle").innerHTML = seconds;
            }, 1000);
            setTimeout(function ()
            {

                //Show Popup before 20 seconds of timeout.
                $.ajax({
                    type: "POST",
                    url: "Admin.Master/logout_timeout",
                    data: "{}",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: OnSuccess,
                    error: OnError
                });
                $find("mpeTimeout").show();
            }, timeout - 20 * 1000);
            setTimeout(function () {
                window.location = "Login.aspx";

                //$.ajax(
                //                  {
                //                      type: "POST",
                //                      contentType: "application/json; charset=utf-8",
                //                      url: "Case.aspx/removeTrans",
                //                      data: "{ 'DelData':'" + transId + "'}",
                //                      success: function (result) { window.location.reload(true); console.log("successful!"); }
                //                  })



            }, timeout);
        };

1 个答案:

答案 0 :(得分:0)

将logout_timeout函数移动到xxxx.aspx并调用Ajax URL&#34; xxxx.aspx / logout_timeout&#34;代替。