簡體   English   中英

JavaScript超時在aspx C#頁面中導致回發-0x800a1391 – JavaScript運行時錯誤

[英]JavaScript timeout causing postback in aspx C# page - 0x800a1391 – JavaScript runtime error

以下JS代碼顯示了剩余時間,但是在通過按“是”按鈕(aspx按鈕)重置時間時會導致回發。 因此,當計時出現在屏幕上並單擊“是”時,它將使主頁上的gridview變為空白,並且如果主模式彈出窗口打開了,它將關閉它,從而使用戶丟失任何內容。 有什么想法如何修改它,以避免在單擊“是”時發回郵件? 還是更好的建議?

碼:

    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;
}

來自以下方面的組合解決方案: 如何以編程方式重置會話時間,而無需在ASP.Net中刷新頁面,而在ASP.Net中 會話過期之前顯示“會話超時”消息

我找到了一個更好的解決方案,可以與第一個解決方案結合使用。 這是修改后的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'未定義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM