在网站中添加一个Global.asax全局应用程序文件.
Global.asax
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
//应用程序启动时运行的代码
Application["count"] = 0;
}
void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
}
void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
}
void Session_Start(object sender, EventArgs e)
{
//对Appliaction加锁以防止并行性
Application.Lock();
//增加一个在线人数
Application["count"] = (int)Application["count"] + 1;
//解锁
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
Application.Lock();
//减少一个在线人数
Application["count"] = (int)Application["count"] - 1;
Application.UnLock();
}
</script>
default.aspx
<%@ Page Language="C#" %>
<!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>
<%Response.Write(Application["count"]); %>
</div>
</form>
</body>
</html> |