统计在线人数
如何運用內置對象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>
?
轉載于:https://www.cnblogs.com/dingxiaowei/archive/2012/10/22/3058839.html
總結
- 上一篇: 设备管理学习之概念篇
- 下一篇: 使用HTML5技术开发的超酷颜色选择器