在线人员统计,Global.asax:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
namespace CountPerson
{
publicclass Global: System.Web.HttpApplication
{
protectedvoid Application_Start(objectsender, EventArgs e)
{
#region OnlineUsers
try
{
DataTable userTable = newDataTable();
userTable.Columns.Add("SessionID");
userTable.Columns.Add("UserIP");
userTable.Columns.Add("Browser");
userTable.Columns.Add("OSName");
userTable.AcceptChanges();
Application.Lock();
Application["OnlineUsers"] = userTable;
Application.UnLock();
}
catch
{
}
#endregion
}
protectedvoid Session_Start(objectsender, EventArgs e)
{
try
{
string sessionid = Session.SessionID;
string userIP = Request.UserHostAddress;
HttpBrowserCapabilities bc = Request.Browser;
string osName = "win2000";
//判断操作系统
switch (bc.Platform)
{
case "WinNT5.1":
case "WinXP":
osName = "Windows XP";
break;
case "WinNT 5.0":
osName = "Win2000";
break;
case "WinNT":
osName = "Win2003";
break;
default:
osName = bc.Platform;
break;
}
string browser = bc.Type;
DataTable userTable = (DataTable)Application["OnlineUsers"];
if (userTable == null)
return;
DataRow[] curRow = userTable.Select("SessionID='" + sessionid + "'");
if (curRow.Length == 0)
{
DataRow row = userTable.NewRow();
row["SessionID"] = sessionid;
row["UserIP"] = userIP;
row["Browser"] = browser;
row["OSName"] = osName;
userTable.Rows.Add(row);
userTable.AcceptChanges();
Application.Lock();
Application["OnlineUsers"] = userTable;
Application.UnLock();
}
}
catch
{
}
}
protectedvoid Application_BeginRequest(object sender, EventArgse)
{
}
protectedvoid Application_AuthenticateRequest(object sender, EventArgse)
{
}
protectedvoid Application_Error(objectsender, EventArgs e)
{
}
protectedvoid Session_End(objectsender, EventArgs e)
{
HashtableonlineUserHashtable = (Hashtable)Application["OnlineUsers"];
onlineUserHashtable.Remove(Request.UserHostAddress);
try
{
string sessionid = Session.SessionID;
DataTable userTable = (DataTable)Application["OnlineUsers"];
if (userTable == null)
return;
foreach (DataRowrow in userTable.Select("SessionID='" + sessionid + "'"))
{
userTable.Rows.Remove(row);
}
userTable.AcceptChanges();
Application.Lock();
Application["OnlineUsers"]= userTable;
Application.UnLock();
}
catch
{
}
}
protectedvoid Application_End(objectsender, EventArgs e)
{
}
}
}
在页面中使用GridView绑定用户列表:
protected void Page_Load(object sender, EventArgse)
{
DataTableuserTable = null;
try
{
userTable = (DataTable)Application["OnlineUsers"];
}
catch
{
Response.Write("获得内存在线数据失败");
}
if(userTable != null)
{
GridView1.DataSource= userTable;
GridView1.DataBind();
}
}
asp.net 在线用户列表统计
最新推荐文章于 2014-04-26 10:10:02 发布