線上使用者列表統計

來源:互聯網
上載者:User
線上人員統計,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();            }        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.