Asp.net Session过期页面处理

本文介绍了一个网页实现自动跳转的功能,并详细解释了前端JavaScript代码和后端ASP.NET代码如何协同工作,确保当用户登录状态失效时能够及时进行页面跳转。文章深入探讨了使用setTimeout函数定时检查会话状态以及页面自动重定向的机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果如下图:

fanxingthink


源码如下:

前端:

<script type="text/javascript">
        var i = 10;

        // 获取登录事件,并处理自动跳转
        window.onload = function windowload() {
            document.getElementById("time").innerText = i;
            document.getElementById("time2").innerText = i;
            JumpPage();
        }

        function JumpPage() {
            document.getElementById("time").innerText = i;
            document.getElementById("time2").innerText = i;
            // 计时为0后,立即跳转
            i--;
            if (i < 0) {
                location.replace("Login.aspx");
            }

            setTimeout("JumpPage()", 1000);
        }
    </script>

<div class="DIV1" style="font-family: 微软雅黑; font-size: small">
    <div class="DIV2" style="color:White;">错误提示:网页信息已过期,请重新登录!(Error: web information has expired, please login again.)</div>
    <div style="height:7px"></div>
        本页将在<span id="time" style="color: Blue"></span>秒后自动跳转至<a href="Login.aspx">登录页面</a>,如需立即跳转,请直接点击 <a href="Login.aspx" style="color: Blue">立即跳转>></a><br/>
        The page will be re-directed in <span id="time2" style="color: Blue"></span> seconds. Please wait a moment...  <a href="Login.aspx" style="color: Blue">Re-directed Now>></a>
        </div>

后端处理:在BasePage的Load事件中判断Session是否为空

if (Session["LoginEmployeeID"] == null)
            {
                Response.Clear();
                Response.Write("<script language=javascript>parent.location.href='../Account/SessionExpired.aspx'</script>");
                //Response.Write("<script language=javascript>window.open('../Account/SessionExpired.aspx');top.opener=null;top.close();</script>");
                Response.End();
            }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值