向站点主站添加标签以显示会话剩余时间 [英] adding label to site master to show time left for session

查看:58
本文介绍了向站点主站添加标签以显示会话剩余时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我试图以分钟为单位显示会议倒计时。会话在web.config中设置10分钟。我希望在主页面上显示lbl,以便在几分钟内显示10,9,8 .......



这里是.cs的代码



 protected void Page_Load (对象发送者,EventArgs e)
{

if(!IsPostBack)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),onLoad, DisplaySessionTimeout(),true);
}

}





和.master文件:



 <   script     type   =  text / javascript >  
var sessionTimeout =<%= Session.Timeout%>

函数DisplaySessionTimeout()
{
document.getElementById(<% = lblSessionTime.ClientID%> ).innerText = sessionTimeout;
sessionTimeout = sessionTimeout - 1;

if(sessionTimeout> = 0)
window.setTimeout(DisplaySessionTimeout(),60000);
else
{
alert(你当前的会话结束了。);
}
}
< / script >

< div >
< h2 > 剩余会话时间:< asp:标签 ID = lblSessionTime runat = server > < / asp:标签 > 分钟。< / h2 >
< hr / >
< / div >





谢谢。

解决方案

这种方法不起作用,因为每次页面加载会话幻灯片并重置超时时。



例如,如果你的会话被设置为30分钟而某人第一次加载页面然后在30分钟内,如果没有活动,会话将超时。但是,对于运行页面的C#代码必须回发,如果在10分钟后发生,则会话重置并在30分钟内超时,而不是在20分钟内。



这样做的方法是使用JavaScript中的setTimeout()每分钟运行一次,每分钟该函数将递减1.这不是保证,因为您没有联系服务器以获得真正的超时,但它是粗暴地尝试做一些技术上不可能的事情。



所以,你可以做这样的事情(只是一个你需要完成的简单例子) :

 


document )。ready( function (){
// SessionTimeout的初始调用
SessionTimeout( 30 );
});

function SessionTimeout( var minutesLeft)
{

#lblTimeout)。val(minutesLeft); // 在标签中显示数字
// 在1分钟内再次调用该函数并递减计数器
setTimeout( function (){SessionTimeout(minutesLeft--);}, 1000 );
}


Hi,

I am trying to show countdown by minute for the session. the session is set in the web.config for 10 minutes. I want to have on the master page the lbl to show 10, 9 , 8, ... in minutes.

here is the code for .cs

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
               Page.ClientScript.RegisterStartupScript(this.GetType(), "onLoad",         "DisplaySessionTimeout()", true);
    }

}



and in the .master file:

<script type="text/javascript">
        var sessionTimeout = <%= Session.Timeout %>
        
        function DisplaySessionTimeout()
        {
            document.getElementById("<%= lblSessionTime.ClientID %>").innerText = sessionTimeout;
            sessionTimeout = sessionTimeout - 1;
            
            if (sessionTimeout >= 0)
                window.setTimeout("DisplaySessionTimeout()", 60000);
            else
            {
                alert("Your current Session is over.");
            }
        }
    </script>

       <div>
          <h2> Session time left: <asp:Label ID="lblSessionTime" runat="server"></asp:Label> minutes.</h2>
       <hr />
       </div>



Thanks.

解决方案

This approach will not work because every time your page loads the Session slides and the timeout is reset.

For example, if your session is set to 30 minutes and someone firsts loads the page then in 30 minutes the session will timeout if there is no activity. However, for your C# code to run the page must postback and if that happens after 10 minutes the session is reset and will timeout in 30 minutes, not in 20 minutes.

The way to do this is by using setTimeout() in JavaScript to run once a minute and at each minute the function will decrement 1. This is not a guarantee because you are not contacting the server to get the true timeout, but it is a crude attempt at doing something that technically isn't possible.

So, you could do something like this (just a simple example that you'll need to finish):


(document).ready(function () { // initial call of SessionTimeout SessionTimeout(30); }); function SessionTimeout(var minutesLeft) {


("#lblTimeout").val(minutesLeft); // display the number in a label // call the function again in 1 minute and decrement the counter setTimeout(function(){SessionTimeout(minutesLeft--);}, 1000); }


这篇关于向站点主站添加标签以显示会话剩余时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆