统计在线人数

如何运用内置对象session和application对象实现统计在线人数

首先创建一个全局应用程序类Global.asax

 

protected void Application_Start(object sender, EventArgs e)
        {
            if (Application["CountOnline"] == null)
            {
                Application["CountOnline"] = 0;
            }       
        }
        //设置session对象,生存时间为1分钟
        protected void Session_Start(object sender, EventArgs e)
        {
            Session.Timeout = 1;
            int countOnline = (int)Application["CountOnline"];//获取在线人数
            countOnline++;
            Application["CountOnline"] = countOnline;//设置当前在线人数

        }

//会话对象销毁的时候,Session对象减1
        protected void Session_End(object sender, EventArgs e)
        {
            int countOnline = (int)Application["CountOnline"];//获取当前在线人数
            countOnline--;
            Application["CountOnline"] = countOnline;
        }

 

其次在页面中绑定

<div>
    当前在线人数为:<%=Application["CountOnline"] %>
    </div>

 

你可能感兴趣的:(session,object,null,application,div)