在aspx C#页面中导致回发的JavaScript超时 - 0x800a1391 - JavaScript运行时错误

时间:2017-04-18 18:04:17

标签: javascript c# asp.net timeout

以下JS代码显示剩余时间,但是当按下Yes按钮(aspx按钮)重置时间时会导致回发。因此,当时间显示在屏幕上,然后单击是时,它将在主页面中清空网格视图,如果主模式弹出窗口已启动,它将关闭它,使用户丢失任何内容。有没有想过如何修改它以避免点击时回发?还是更好的建议?

代码:

    function SessionExpireAlert(timeout) {
    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.
        $find("mpeTimeout").show();
    }, timeout - 20 * 1000);
    setTimeout(function () {
        window.location = "Login.aspx";
    }, timeout);
};
function ResetSession() {
    //Redirect to refresh Session.
    window.location = window.location.href;
}

以下组合解决方案: How to programatically reset the session time without page refreshing in ASP.Net Display Session Timeout message before Session expires in ASP.Net

1 个答案:

答案 0 :(得分:0)

我找到了一个更好的解决方案,我结合了第一个。这是修改后的JS代码:

    var interval;
function SessionExpireAlert(timeout) {
    clearInterval(interval);
    var seconds = timeout / 1000;
    document.getElementById("seconds").innerHTML = seconds;
    document.getElementById("secondsIdle").innerHTML = seconds;
    interval = setInterval(function () {
        seconds--;
        document.getElementById("seconds").innerHTML = seconds;
        document.getElementById("secondsIdle").innerHTML = seconds;

    }, 1000);

    setTimeout(function () {
        //Show Popup before 50 seconds of timeout.
        $find("mpeTimeout").show();
    }, timeout - 50 * 1000);
    //if (seconds == 0) {
    //    window.location = "Login.aspx";
    //}
    setTimeout(function () {
        window.location = "Login.aspx";
    }, timeout);
};
function ResetSession() {
    PageMethods.ResetSession(OnSuccess);
    return false;
}
function OnSuccess(response, userContext, methodName) {
    SessionExpireAlert(response);
}

这里是ASPX代码:

<%-- FOR TIME OUT --%>

        <script type="text/javascript" src="js/timeOutScript.js"></script>
        <h3>Session Idle:&nbsp;<span id="secondsIdle"></span>&nbsp;seconds.</h3>
        <asp:LinkButton ID="lnkFake3" runat="server" />
        <ajax:ModalPopupExtender ID="mpeTimeout" BehaviorID ="mpeTimeout" runat="server" PopupControlID="pnlPopupmpeTimeout" TargetControlID="lnkFake3"
            OkControlID="btnYes" CancelControlID="btnNo" BackgroundCssClass="modalBackground3" OnOkScript = "ResetSession()">
        </ajax:ModalPopupExtender>
        <asp:Panel ID="pnlPopupmpeTimeout" runat="server" CssClass="modalPopup3" Style="display: none">
            <div class="header">
                Session Expiring!
            </div>
            <div class="body">
                Your Session will expire in&nbsp;<span id="seconds"></span>&nbsp;seconds.<br />
                Do you want to continue?
            </div>
            <div class="footer" align="right">
                <asp:Button ID="btnYes" runat="server" Text="Yes" CssClass="yes3" OnClick="btnYes_Click" OnClientClick="return ResetSession()"/>
                <asp:Button ID="btnNo" runat="server" Text="No" CssClass="no3" OnClick="btnNo_Click"/> 
            </div>
        </asp:Panel>

        <%-- ENDS TIME OUT --%>

这里的C#代码背后:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    lbUserID.Text = Context.User.Identity.Name;
    UpdateProgress1.AssociatedUpdatePanelID = UpdatePanel1.UniqueID;
    lbLocalTime.Text = DateTime.Now.ToLongDateString();
    if (!this.IsPostBack)
    {

        /*FOR TIMEOUT PURPOSE*/

        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;
        int timeout = GetSessionTimeout();
        ClientScript.RegisterStartupScript(this.GetType(), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true);

        /*ENDS TIME OUT*/

    }

}

[WebMethod(EnableSession = true)]
public static int ResetSession()
{
    HttpContext.Current.Session["Reset"] = true;
    int timeout = GetSessionTimeout();
    return timeout;
}

private static int GetSessionTimeout()
{
    Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
    SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState");
    return Convert.ToInt32(section.Timeout.TotalMinutes * 1000 * 60);
}

标签:0x800a1391 - JavaScript运行时错误:'PageMethods'未定义。