1.首先在项目中添加一个  “全局应用程序类”   Global.asax

            代码如下:

           

                     <script runat="server">

                       void Application_Start(object sender, EventArgs e)
                          {
                             //在应用程序启动时运行的代码
                             Application["total"] = 0;
                             Application["online"] = 0;
                          }

                       void Application_End(object sender, EventArgs e)
                         {
                             //在应用程序关闭时运行的代码

                          }

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

                         }

                       void Session_Start(object sender, EventArgs e)
                         {
                            //在新会话启动时运行的代码
                            Application["total"] = (int)Application["total"] + 1;
                           Application["online"] = (int)Application["online"] + 1;
                         }

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

                   </script>

2.  在Web.confing   的 <system.web>标签下添加如下代码

                      < sessionState  mode="InProc">

                      </sessionState>

3.在Web页面中添加  “安全退出按钮”  退出Session

      protected void Button2_Click(object sender, EventArgs e)

          { 

                Session.Abandon();

         }

 

4.在WEB页面中输出Application的值

   protected void Page_Load(object sender, EventArgs e)

       { 

            Response.Write(”当前在线人数为“+Application["online"].ToString());

            Response.Write("总访问次数:"+Application["total"].ToString());

       } 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-23
  • 2021-09-21
  • 2021-10-07
相关资源
相似解决方案