【问题标题】:JavaScript timeout causing postback in aspx C# page - 0x800a1391 – JavaScript runtime errorJavaScript 超时导致 aspx C# 页面中的回发 - 0x800a1391 – JavaScript 运行时错误
【发布时间】:2017-09-14 17:05:21
【问题描述】:

以下 JS 代码显示剩余时间,但在通过按是按钮(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

【问题讨论】:

  • 您的ResetSession() 函数显式刷新页面。您甚至在该函数的评论中这么说。如果您不希望它那样做,那么我猜它会做任何您 想要它做的事情?究竟是什么?

标签: javascript c# asp.net timeout


【解决方案1】:

我找到了一个更好的解决方案,并与第一个解决方案相结合。这是修改后的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”未定义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    相关资源
    最近更新 更多