Use the Application object in ASP. NET to implement the simple online count statistics function

Source: Internet
Author: User

Use the Application object in ASP. NET to implement the simple online count statistics function

This article describes how to use the Application object in ASP. NET to implement the simple online count statistics function. This article provides implementation steps and corresponding code examples. For more information, see

Note: I am reviewing ASP. NET recently. I will make some demo programs to help you better understand the situation and share them with you.

1. Create an ASP. NET Website and edit the Global. asax file. The modified file content is as follows.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

<% @ Application Language = "C #" %>

 

<Script runat = "server">

 

Void Application_Start (object sender, EventArgs e)

{

// Code that runs when the application starts

Application ["CurrentUserCount"] = 0;

}

 

Void Application_End (object sender, EventArgs e)

{

// Code that runs when the application is closed

 

}

 

Void Application_Error (object sender, EventArgs e)

{

// Code that runs when an unhandled error occurs

 

}

 

Void Session_Start (object sender, EventArgs e)

{

// The code that runs when the new session starts

Application. Lock ();

Application ["CurrentUserCount"] = (int) Application ["CurrentUserCount"] + 1;

Application. UnLock ();

}

 

Void Session_End (object sender, EventArgs e)

{

// The code that runs when the session ends.

// Note: The Session_End event is triggered only when the sessionstate mode in the Web. config file is set to InProc.

// If the session mode is set to StateServer

// Or SQLServer, the event is not triggered.

Application. Lock ();

Application ["CurrentUserCount"] = (int) Application ["CurrentUserCount"]-1;

Application. UnLock ();

}

 

</Script>

2. Modify the Web. config file, add the following configuration nodes, and add new configuration nodes. Node.

The Code is as follows:

  

3. Add a label to the Default. aspx file to display the current number of online users.

The Code is as follows:

Protected void Page_Load (object sender, EventArgs e)

{

This. Label1.Text = Application ["CurrentUserCount"]. ToString ();

}

4. Use IE and Chrome to access the application. The result is displayed.

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.