You can use Global. asax Global variables to avoid refreshing counters on the page.
Void Application_Start (object sender, EventArgs e)
{
// The code that runs when the application is started, and the counter is refreshed when IIS is restarted.
Application ["count"] = 0;
}
Void Session_Start (object sender, EventArgs e)
{
// The code that runs when the new session starts. The session is established and the counter is + 1 to prevent the error from being locked first and then unlocked. Refresh the page repeatedly without changing the counter. You can only create a new session.
Application. Lock ();
Application ["count"] = (int) Application ["count"] + 1;
Application. UnLock ();
}
Void Session_End (object sender, EventArgs e)
{
// The code that runs when the session ends. This is mainly used to count the number of online users. If you only need to count, you do not need the code here.
// Note: Only the sessionstate mode in the Web. config file is set
// The Session_End event is triggered only when InProc is used. If the session Mode
// If it is set to StateServer or SQLServer, this event is not triggered.
Application. Lock ();
Application ["count"] = (int) Application ["count"]-1;
Application. UnLock ();
}
Protected void Page_Load (object sender, EventArgs e)
{
// Directly use Application ["count"] on the page.
Response. Write ("you are the website" + Application ["count"] + "visitor ");
}
Note: The difference between Application and Session. The former is the application variable, and the latter is the session variable. The former remains for a long time and cannot be used in disorder.