asp.net实现显示在线会员

 

asp.net中:
global.asax.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;

namespace alumniV2
{
 /// <summary>
 /// Global 的摘要说明。
 /// </summary>
 public class Global : System.Web.HttpApplication
 {
  public Global()
  {
   InitializeComponent();
  } 
  
  protected void Application_Start(Object sender, EventArgs e)
  {
   Application.Lock ();
   
   Application["usernum"]=0;
   Application["onlineuser"]="";   
   Application["guestnum"]=0;

   Application.UnLock ();
  }
 
  protected void Session_Start(Object sender, EventArgs e)
  {
   Application.Lock ();

   Application["guestnum"]=(Int32)Application["guestnum"]+1;

   Application.UnLock ();
  }

  protected void Application_BeginRequest(Object sender, EventArgs e)
  {

  }

  protected void Application_EndRequest(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)
  {
   Application.Lock ();
   string name;
   if (Request.Cookies ["personal"]!=null )
   {
    name=Request.Cookies ["personal"]["username"] ;
    if (name!="")
    {
      Application["usernum"]=(Int32)Application["usernum"]-1;

      Application["onlineuser"]=(Application["onlineuser"]+"").Replace (name+"$","");

    }
    else
     Application["guestnum"]=(Int32)Application["guestnum"]-1;
   }
   else
    Application["guestnum"]=(Int32)Application["guestnum"]-1;
   Application.UnLock ();
  }

  protected void Application_End(Object sender, EventArgs e)
  {
   Application.Lock ();
   
   Application["usernum"]=0;
   Application["onlineuser"]="";   
   Application["guestnum"]=0;

   Application.UnLock ();
  }
   
  #region Web Form Designer generated code
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
  }
  #endregion
 }
}

index.aspx.cs

  private void ShowOnlineUser()
  {
   onlinenum.Text ="  当前共有 <font color=red>"+ Application["usernum"] +"</font>  个成员 <font color=red>"+ Application["guestnum"] +"</font>  个客人在线   <img src='images/user.gif'> <font color=red>成员</font>   <img src='images/guest.gif'> 客人 ";

   string oluserstr;
   string []oluser;
   int n;
   oluserstr="";
   if (Application["onlineuser"]!=null)
   {
    //oluser1=;
    oluser=(Application["onlineuser"]+"").Split('$');
    for (n=0;oluser[n]!="" && oluser[n]!=null;n++)
     oluserstr=oluserstr+"  <img src='images/user.gif'> <a href='myinfo.aspx?username="+Server.UrlEncode(oluser[n])+"'><font color=red>"+oluser[n]+"</font></a> ";
   }
    
   if (Application["guestnum"]!=null)
   {
    for (n=0;n<(Int32)Application["guestnum"];n++)
     oluserstr=oluserstr+"  <img src='images/guest.gif'> 客人 ";
   }
   onlineuser.Text =oluserstr;
  }

你可能感兴趣的:(asp.net)