web程序记录当前在线人数

在页面上显示当前在线人数

效果:

1、Global.asax文件:

<%@ Application Language=”C#” %>
<%@ Import Namespace=”System.Xml” %>
<script runat=”server”>

    void Application_Startobject sender, EventArgs e)
    {
        // 在应用程序启动时运行的代码
        //载入配置文档
        XmlDocument AppConfig = new XmlDocument);
        AppConfig.LoadHttpContext.Current.Server.MapPath”~/AppConfig.xml”));

        //读取配置信息
        XmlNode xnList = AppConfig.GetElementsByTagName”dblist”)[0];
        XmlElement xeDb1 = XmlElement)xnList.ChildNodes[0];
        XmlElement xeDb2 = XmlElement)xnList.ChildNodes[1];
        Application[“db1user”] = xeDb1.GetAttribute”user”);
        Application[“db1source”] = xeDb1.GetAttribute”source”);
        Application[“db2user”] = xeDb2.GetAttribute”user”);
        Application[“db2source”] = xeDb2.GetAttribute”source”);
        Application[“count”] = 0;
    }
    
    void Application_Endobject sender, EventArgs e)
    {
        //在应用程序关闭时运行的代码

    }
        
    void Application_Errorobject sender, EventArgs e)
    {
        //在出现未处理的错误时运行的代码

    }

    void Session_Startobject sender, EventArgs e)
    {
        //在新会话启动时运行的代码
        Application[“count”] = Convert.ToInt32Application[“count”])+1;
        
    }

    void Session_Endobject sender, EventArgs e)
    {
        //在会话结束时运行的代码。
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式
        //设置为 StateServer 或 SQLServer,则不会引发该事件。
        Application[“count”] = Convert.ToInt32Application[“count”]) – 1;
    }
       
</script

———————–

2、显示页面:

前台:

 <span  id=”Span1″ runat=”server” class=”bottom”>    </span>

后台:

 Span1.InnerHtml = “当前在线” + Application[“count”].ToString) + “人”;

Published by

风君子

独自遨游何稽首 揭天掀地慰生平