• 基于Global.asax实现显示当前在线人数--ASP.NET基础


    相对来说比较简单,直接贴代码了哈:

    Global.asax:

    <%@ Application Language="C#" %>
    <script runat="server">
        void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            Application.Add("visitor", 0);//创建一个Application对象变量,变量名为visitor,变量值为0
        }
        void Application_End(object sender, EventArgs e) 
        {
            //  在应用程序关闭时运行的代码
        }
        void Application_Error(object sender, EventArgs e)
        {
            // 在出现未处理的错误时运行的代码
        }
        void Session_Start(object sender, EventArgs e)
        {
            // 在新会话启动时运行的代码
            Application.Lock();//锁定application对象变量,防止修改冲突
            Application["visitor"] = (int)Application["visitor"] + 1;//这里需要进行数据类型转换,Object类型转为Int
            Application.UnLock();//解锁application对象变量
        }
        void Session_End(object sender, EventArgs e)
        {
            // 在会话结束时运行的代码。
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
            // 或 SQLServer,则不引发该事件。
            Application.Lock();//锁定application对象变量,防止修改冲突
            Application["visitor"] = (int)Application["visitor"] - 1;//这里需要进行数据类型转换,Object类型转为Int
            Application.UnLock();//解锁application对象变量
        }   
    </script>


    index.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="administor_index3" %>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <%
            Response.Write("当前在线人数为:" + Application["visitor"].ToString()); //Application["visitor"]对象转换为字符串类型
         %>
        </div>
        </form>
    </body>
    </html>

     

  • 相关阅读:
    window.clearInterval与window.setInterval的用法
    <a href=“#”>
    DIV+CSS 中的 overflow:hidden
    @media screen
    VS2010程序打包操作--超详细
    Easy CHM使用简明教程
    垃圾纸盒的叠法-超级实用
    IOS 3D UI --- CALayer的transform扩展
    CGContextAddArc
    CGPathAddArc
  • 原文地址:https://www.cnblogs.com/qikeyishu/p/7813666.html
Copyright © 2020-2023  润新知