您的位置:首页 > 编程语言 > ASP

ASP.net中如何让Session永不过期

2007-08-12 12:29 603 查看
作者首先借鉴了一篇文章中所提到的定时刷新方法,把如下所示的代码放到Page_Load()事件:

private void ReconnectSession()
{
int int_MilliSecondsTimeOut = (this.Session.Timeout * 60000) - 30000;
string str_Script = @"
<script type='text/javascript'>
function Reconnect()
{
window.open('reconnect.aspx','reconnect','width=5,height=5,top=1800,
left=1800,scrollbars=no,showintaskbar=no');
}
window.setInterval('Reconnect()',"+int_MilliSecondsTimeOut.ToString()+ @");
</script>";
this.Page.RegisterClientScriptBlock("Reconnect", str_Script);
}
考虑到如果项目中包含很多页面,给每个页面都拷上这样的代码也是一个力气活,作者决定新建一个类,这个类继承自System.Web.UI.Page:
public class YourNewPageClass : System.Web.UI.Page
然后改写YourNewPageClass类的OnInit()事件: override protected void OnInit(EventArgs e)
{
base.OnInit(e);
if (Context.Session != null)
{
int int_MilliSecondsTimeOut = (this.Session.Timeout * 60000) - 30000;
string str_Script = @"
<script type='text/javascript'> function Reconnect()
{
window.open('reconnect.aspx','reconnect','width=5,height=5,top=1800,
left=1800,scrollbars=no,showintaskbar=no');
}
window.setInterval('Reconnect()',"+int_MilliSecondsTimeOut.ToString()+ @");
</script>";
this.Page.RegisterClientScriptBlock("Reconnect", str_Script);
}
}
以后新建的页面只要继承这个类就可以实现永不过期的Session了。

更大的问题:弹出窗口被浏览器过滤掉怎么办?

作者终于想到了伟大的万精油武器—Ajax了: function getObjectHTTP()
{
var xmlhttp = null
if (navigator.userAgent.indexOf("MSIE")>=0)
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
}
else
{
xmlHttp = new XMLHttpRequest()
}
return xmlhttp
}

function Reconnect()
{
var url='reconnect.aspx'
xmlHttp=getObjectHTTP()
xmlHttp.open('GET', url , true)
xmlHttp.send(null)
}
看看Reconnect函数的实现,这可是Ajax实现的标准模式哦
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: