Application对象

本文详细介绍了如何在ASP.NET应用中使用Application对象显示在线人数,包括初始化计数、会话开始与结束时的计数操作,以及在网页中显示当前在线人数的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用application对象显示在线人数,Global.asax文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace ManageStudent
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            //在应用程序启动时运行的代码
            //初始化变量:UserCount()
            Application.Lock();//临界变量,使用加锁功能
            Application["UserCount"] = 0;
            Application.UnLock();//临界变量被解锁

        }

        protected void Session_Start(object sender, EventArgs e)
        {
            //在新会话启动时运行的代码
            //用户数量加1
            Application.Lock();
            Application["UserCount"] = Int32.Parse(Application["UserCount"].ToString()) + 1;
            Application.UnLock();
        }

        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)
        {
            //在新会话结束时运行的代码
            //注意:只有在web.config文件中的sessionstate模式设置为InProc时。
            //如果会话模式设置为StateServer或SQLServer,则不会引发该事件
            //用户数量减一
            Application.Lock();
            Application["UserCount"] = Int32.Parse(Application["UserCount"].ToString())- 1;
            Application.UnLock();
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}

接下来,需要在web.config配置文件中<system,web>标签内添加如下一条语句

 <sessionState mode="InProc"/>

在ApplicationDemo.aspx中写入代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ApplicationDemo.aspx.cs" Inherits="ManageStudent.ApplicationDemo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblUserCount" runat="server" Text=""></asp:Label><br />
        <asp:Button ID="btnLogout" runat="server" Text="注销" OnClick="btnLogout_Click" />
    </div>
    </form>
</body>
</html>

 

 

在cs文件中,写入代码,用于显示在线人数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ManageStudent
{
    public partial class ApplicationDemo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lblUserCount.Text = "网站在线人数:" + Application["UserCount"] + "";
            }
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            //销毁Session
            Session.Abandon();
        }
    }
}

 

转载于:https://www.cnblogs.com/niexianxue/p/4439746.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值