Estadísticas de usuarios en línea en ASP.NET


Código de front-end default.aspx

<!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="visitors" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

Código de backend

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

public partial class _Default : System.Web.UI.Page
{
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
    
    
         
        visitors.Text = "本站当前有:<b>" + Application["user_sessions"].ToString() + "</b> 位访问者!";
    }
}

Global.asax

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
    
    
        //在应用程序启动时运行的代码
        
        //初始化默认为0
        Application["user_sessions"] = 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["user_sessions"] = (int)Application["user_sessions"] + 1;
        Application.UnLock();//取消锁定
    }

    void Session_End(object sender, EventArgs e) 
    {
    
    
        //在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式 
        //设置为 StateServer 或 SQLServer,则不会引发该事件。

    }
       
</script>

Supongo que te gusta

Origin blog.csdn.net/qq_33285360/article/details/109269596
Recomendado
Clasificación