Asp. NET in the use of application objects to achieve a simple online count function _ practical skills

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.