zoukankan      html  css  js  c++  java
  • 基于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>

     

  • 相关阅读:
    面试题:你了解对象分配规则吗
    面试题:Java 对象的创建过程
    面试题:Minor GC、Major GC、Full GC的触发时机
    常见的垃圾回收算法
    Java GUI快速入门
    Java Swing 介绍
    Java 开发环境配置
    Java (windows)安装教程
    Gamma、Linear、sRGB 和Unity Color Space,你真懂了吗?
    Qt OpenGL 蒙板
  • 原文地址:https://www.cnblogs.com/qikeyishu/p/7813666.html
Copyright © 2011-2022 走看看