Note: Recently in review asp.net, in order to deepen the impression, will make some small demo program, share to everyone.
1 new asp.net web site, edit the Global.asax file, the modified file as shown below.
<%@ application language= "C #"%> <script "Server" > void runat=
Art (object sender, EventArgs e) {//code that runs at application startup application["Currentusercount"] = 0; } void Application_End (object sender, EventArgs e) {//code that runs when the application shuts down Application_Error (obje CT sender, EventArgs e) {//code to run when an unhandled error occurs} void Session_Start (object sender, EventArgs e) {//
Code Application.Lock () that runs when a new session starts;
application["Currentusercount"] = (int) application["Currentusercount"] + 1;
Application.UnLock ();
} void Session_End (object sender, EventArgs e) {//code that runs at the end of the session.
Note: Session_End events are raised only if the sessionstate mode in the Web.config file is set to InProc.
If the session mode is set to StateServer//or SQL Server, the event is not raised.
Application.Lock ();
application["Currentusercount"] = (int) application["Currentusercount"]-1;
Application.UnLock (); } </script>
2 Modify the Web.config file, add the following configuration nodes, add the configuration node bit <system.web></system.web> node below.
Copy Code code as follows:
<sessionstate mode= "InProc" timeout= "1" cookieless= "false"/>
3 Add a label to the Default.aspx file to display the current number of people online.
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
This. Label1.Text = application["Currentusercount"]. ToString ();
}
4 successively use IE and Chrome browser to access the application, to get the results shown in the following figure.