Javascript 回发刷新服务器超时后在DropDownList中维护页码
我正在使用.Net、VB和webforms。我有一个弹出的计时器,允许用户在会话到期前刷新连接。应用程序中的三个页面使用完全相同的样式Javascript 回发刷新服务器超时后在DropDownList中维护页码,javascript,asp.net,vb.net,Javascript,Asp.net,Vb.net,我正在使用.Net、VB和webforms。我有一个弹出的计时器,允许用户在会话到期前刷新连接。应用程序中的三个页面使用完全相同的样式DropDownList,以控制显示的内容 当用户单击“确定”刷新包含DropDownList的页面时,用户会收到一个错误,即DropDownList只能选择一个项目 如何使回发后的页面呈现为与预弹出页面相同的状态 protectedsub-ddlPages\u选择dexchanged1(ByVal sender作为对象,ByVal e作为System.Even
DropDownList
,以控制显示的内容
当用户单击“确定”刷新包含DropDownList
的页面时,用户会收到一个错误,即DropDownList
只能选择一个项目
如何使回发后的页面呈现为与预弹出页面相同的状态
protectedsub-ddlPages\u选择dexchanged1(ByVal sender作为对象,ByVal e作为System.EventArgs)
'切换到寻呼机中选择的页面
Dim gvrPager As GridViewRow=gvSelectEvents.BottomPageRow
Dim ddlPages As DropDownList=DirectCast(gvrpage.Cells(0).FindControl(“ddlPages”),DropDownList)
gvSelectEvents.PageIndex=ddlPages.SelectedIndex
'填充您的网格
gvSelectEvents.DataBind()
末端接头
var-DoLogout=1;
瓦恩米尔斯;
var定时器;
var-WarntionMills;
var重定向url;
var_timeLeft;
函数StartTimeout(TimeoutValue、WarnValue、WarnDuration、URLValue){
TimeoutMills=TimeoutValue;
WarnMills=WarnValue;
WarnDurationMills=警告持续时间;
重定向URL=URLValue;
setTimeout('UserTimeout()',TimeoutMills);
setTimeout('WarnTimeout()',WarnMills);
}
函数UserTimeout(){
if(DoLogout==1){
top.location.href=重定向URL;
}
否则{
DoLogout=1;
setTimeout('UserTimeout()',TimeoutMills);
setTimeout('WarnTimeout()',WarnMills);
}
}
函数WarnTimeout(){
_timeLeft=(WarnDurationMills/1000);
updateCountDown();
$find('mdlSessionTimeout').show();
}
函数updateCountDown(){
var min=数学楼层(_timeLeft/60);
var sec=_timeLeft%60;
如果(第10节)
秒=“0”+秒;
document.getElementById(“倒计时持有者”).innerHTML=min+“:”+sec;
如果(_timeLeft>0){
_时间限制--;
setTimeout('updateCountDown()',1000);
}
}
函数保存会话(){
DoLogout=0;
__doPostBack('btnPreserveSession','';
}
页
属于
您无需刷新页面即可保持会话处于活动状态。相反,您可以使用AJAX调用.asmx文件中的方法,该文件具有EnableSession:=True
该代码的示例如下:
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class SessionKeepAlive
Inherits System.Web.Services.WebService
' this function is to called every 19 minutes from the Master page
' to keep the session alive.
<WebMethod(EnableSession:=True)> _
<Script.Services.ScriptMethod(UseHttpGet:=True)> _
Public Function Check() As String
If HttpContext.Current.Session("username") Is Nothing Then
Return "expired"
Else
Return "ok"
End If
End Function
End Class
导入System.Web.Services
导入System.Web.Services.Protocols
导入System.ComponentModel
'要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
_
_
_
_
公共类SessionKeepAlive
继承System.Web.Services.WebService
'此函数从母版页开始每19分钟调用一次
'以使会话保持活动状态。
_
_
作为字符串的公共函数Check()
如果HttpContext.Current.Session(“用户名”)为空,则
返回“过期”
其他的
返回“ok”
如果结束
端函数
末级
如果您使用的是
,那么AJAX的JavaScript可能如下所示:
<script type="text/javascript">
//<![CDATA[
var keepAlive = function() {
Sys.Net.WebServiceProxy.invoke("SessionKeepAlive.asmx", "Check", true, {}, SessionKeepAlive_Callback);
}
function SessionKeepAlive_Callback(result, eventArgs) {
if (result != 'ok') {
alert('Your session has timed out.\nPlease log in again.');
window.location = 'login.aspx';
};
}
// Set 19 minute .NET session keep alive timer...
window.setInterval(keepAlive, 19 * 60 * 1000);
//]]>
</script>
//
您可以选择在web服务中调用该方法的不同方式,例如使用jQuery @Rogerduce请随意说出我的答案不适合您的原因-我可能可以提供一个调整或替代方案。我在实施此修复时遇到了多个问题,我现在确实正确地实施了所有修复,但实际上并没有延长会话。请参阅:@Rogerduce您需要一些会话变量,它不必像
会话(“用户名”)
中那样命名为“username”,只是用户成功登录时您设置的内容。@Rogerduce如果您阅读了有关会话的文档,您将看到如何检查错误,这将有助于调试它。@Rogerduce,不客气:)现在Javascript已经可以使用了,您可能希望它能够对其进行调整,以便在(比如)4小时后强制注销那些在回家时不关机的人。