分享经验,共同进步.

专注.NET开发,涉及Silverlight、WPF、ArcGIS、Android、Windows Phone等技术开发!

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();

            }

        }


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/taomanman/article/details/7066899
个人分类: C#.NET编程
所属专栏: asp.net 应用开发
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

asp.net 在线用户列表统计

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭