我在网站首页里写入js<script runat="server" type="text/javascript">
   
        void Application_Start(object sender,EventArgs e)
        {
        Application["counter"]=0;
        }        void Session_Start(object sender,EventArgs e)
        {
        Application.Lock();
            if (Application["counter"] == null)
            {
                Application["counter"] = 0;
            }
            Application["counter"]=(int)Application["counter"]+1;
            
            Application.UnLock();
        }        void Session_End(object sender,EventArgs e)
        {
        Application.Lock();
        Application["counter"]=(int)Application["counter"]-1;        Application.UnLock();
        }
</script>
首页尾部输出
<div>
   浏览人数: <% Response.Write(Application["counter"]);%>
</div>
但在浏览页面时无法显示人数,是哪里错了呢?

解决方案 »

  1.   

    不是你这样。我有源代码`要的话QQM我81835955
      

  2.   

    给你段代码看看:在线人员统计,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
    {
        public class Global : System.Web.HttpApplication
        {        protected void Application_Start(object sender, EventArgs e)
            {
                #region OnlineUsers
                try
                {
                    DataTable userTable = new DataTable();
                    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
            }        protected void Session_Start(object sender, EventArgs e)
            {
                try
                {
                    string sessionid = Session.SessionID;
                    string userIP = Request.UserHostAddress;
                    HttpBrowserCapabilities bc = Request.Browser;
                    string osName = "win2000";
                    //判断操作系统
                    switch (bc.Platform)
                    {
                        case "WinNT 5.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
                { 
                }
            }        protected void Application_BeginRequest(object sender, EventArgs e)
            {        }        protected void Application_AuthenticateRequest(object sender, EventArgs e)
            {        }        protected void Application_Error(object sender, EventArgs e)
            {        }        protected void Session_End(object sender, EventArgs e)
            {
                Hashtable onlineUserHashtable = (Hashtable)Application["OnlineUsers"];
                onlineUserHashtable.Remove(Request.UserHostAddress);
                try
                {
                    string sessionid = Session.SessionID;
                    DataTable userTable = (DataTable)Application["OnlineUsers"];
                    if (userTable == null)
                        return;
                    foreach (DataRow row in userTable.Select("SessionID='" + sessionid + "'"))
                    {
                        userTable.Rows.Remove(row);
                    }
                    userTable.AcceptChanges();
                    Application.Lock();
                    Application["OnlineUsers"]= userTable;
                    Application.UnLock();
                }
                catch
                { 
                }
            }        protected void Application_End(object sender, EventArgs e)
            {        }
        }
    }在页面中使用GridView绑定用户列表:
    protected void Page_Load(object sender, EventArgs e)
            {
                DataTable userTable = null;
                try
                {
                    userTable = (DataTable)Application["OnlineUsers"];
                }
                catch
                {
                    Response.Write("获得内存在线数据失败");
                }
                if (userTable != null)
                {
                    GridView1.DataSource = userTable;
                    GridView1.DataBind();
                }
            }