asp.net 在线用户列表统计

在线人员统计,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();

            }

        }


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值