我试图在ASP.NET中执行会话状态,但是它总是在不同页面中从1开始。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["count"] = 0;
}
int count = (int)Session["count"];
count++;
Label1.Text = count.ToString();
Session["count"] = count;
}
// In Global.asax
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
}
void Session_Start(object sender, EventArgs e)
{ // start of Session
Session["count"] = 0;
}
void Session_End(object sender, EventArgs e)
{
}
}
我希望计数继续,但在重新访问该网络表单后不会从1开始。谢谢!
答案 0 :(得分:0)
如果要显示站点上的活动用户总数,请使用应用程序对象代替会话对象,并在session_start上增加计数,在session_end上减少计数。
protected void Application_Start(object sender, EventArgs e)
{
Application["count"] = 0;
}
void Session_Start()
{ // start of Session
Application["count"] += 1;
}
void Session_End()
{
Application["count"] -= 1;
}