通过显示在线人数从而得知当前用户的数量
.aspx文件代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace test
{
public partial class WebForm1 : System.Web.UI.Page
{
//下载页面时,将Application["user_sessions"]状态的值取出
protected void Page_Load(object sender, EventArgs e)
{
string strCounter = Application["user_sessions"].ToString();
string imgName;
Response.Write("本站当前有");
for (int i = 0; i < strCounter.Length; i++)
{
imgName = strCounter.Substring(i, 1);
Response.Write(imgName);
}
Response.Write("位访问者");
}
}
}
global.asax文件:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
namespace test
{
public class application : System.Web.HttpApplication
{
//第一位访客链接到应用程序时,触发Application-Start事件,以一个在整个
//"应用程序范围"的Application对象Application["user_sesions"]将在线人数置0
protected void Application_Start(object sender, EventArgs e)
{
Application["user_sessions"] = 0;
}
//每一位访客链接页面时,触发Session-Start事件创建自己的会话状态。
//为Application["user_sessions"]状态加1
protected void Session_Start(object sender, EventArgs e)
{ //锁定应用程序状态
Application.Lock();
//为应用程序状态加1
Application["user_sessions"]=(int)Application["user_sessions"]+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)
{
}
//访客断开链接或访问超时时,觖发Session-End事件,为访客断开会话状态。
//在此过程中,Application["user_sessions"]状态减1
protected void Session_End(object sender, EventArgs e)
{
Application.Lock();
Application["user_sessions"] = (int)Application["user_sessions"] - 1;
Application.UnLock();
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
.aspx文件代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace test
{
public partial class WebForm1 : System.Web.UI.Page
{
//下载页面时,将Application["user_sessions"]状态的值取出
protected void Page_Load(object sender, EventArgs e)
{
string strCounter = Application["user_sessions"].ToString();
string imgName;
Response.Write("本站当前有");
for (int i = 0; i < strCounter.Length; i++)
{
imgName = strCounter.Substring(i, 1);
Response.Write(imgName);
}
Response.Write("位访问者");
}
}
}
global.asax文件:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
namespace test
{
public class application : System.Web.HttpApplication
{
//第一位访客链接到应用程序时,触发Application-Start事件,以一个在整个
//"应用程序范围"的Application对象Application["user_sesions"]将在线人数置0
protected void Application_Start(object sender, EventArgs e)
{
Application["user_sessions"] = 0;
}
//每一位访客链接页面时,触发Session-Start事件创建自己的会话状态。
//为Application["user_sessions"]状态加1
protected void Session_Start(object sender, EventArgs e)
{ //锁定应用程序状态
Application.Lock();
//为应用程序状态加1
Application["user_sessions"]=(int)Application["user_sessions"]+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)
{
}
//访客断开链接或访问超时时,觖发Session-End事件,为访客断开会话状态。
//在此过程中,Application["user_sessions"]状态减1
protected void Session_End(object sender, EventArgs e)
{
Application.Lock();
Application["user_sessions"] = (int)Application["user_sessions"] - 1;
Application.UnLock();
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}