Asp.net Application

Source: Internet
Author: User

Syntax of the application object to read data:
'Read
Data = application ("Data name ")
'Write
Application ("Data name") = Data
Example:
Dim I
Application (I) = application (I) + 1

Non-synchronous update of application data:
Application. lock' lock Object
Application (I) = application (I) + 1
Application. unlock' unlock

Application lifecycle:
Starting from iis, stopping from iis or shutting down


If there are two people: A and A are already visiting a website
1.
Sessin ["userid"] = "A"; // indicates the page on which wenlun jumps as long as a is not logged out, string str = session ["userid"] stores the initialization data. It is often used in the page_load event to authenticate the identity.

Page_load ()
{
If (session ["userid"] = null | session ["userid"] = "")
{
Response. redirect ("Logon page, log on again ");
}
Else
{
Display page
}
}

2.
Application ["x"];
If a value is assigned, such as application ["x"];, a and a can already share the value, which can be used as a counter to record site visits.
As long as a user logs on, application ["x"] + = 1; // Add a number, indicating that a new user has just logged on
3.
This. viewstate ["y"] = "abcde ";
This indicates that the value of this. viewstate ["y"] can be used as a global variable on this page, as long as you do not exit this page,
Including page refresh, it will not be lost

Applications can store common variables or objects containing large amounts of data.

1 void application_start (object sender, eventargs e)
2 {
3 // code that runs when the application starts
4 application ["count"] = 0;
5 system. io. filestream fs = system. io. file. open (server. mappath ("count.txt"), system. io. filemode. openorcreate );
6 system. io. streamreader sr = new system. io. streamreader (fs );
7 application ["allusers"] = convert. toint32 (sr. readline ());
8 sr. close ();
9 fs. close ();
10}
11 void application_end (object sender, eventargs e)
12 {
13 // code that runs when the application is closed
14
15}
16 void application_error (object sender, eventargs e)
17 {
18 // The generation that runs when an unhandled error occurs
19
20}
21 void session_start (object sender, eventargs e)
22 {
23 // the code that runs when the new session starts
24 application. lock ();
25 application ["count"] = convert. toint32 (application ["count"]) + 1;
26 application ["allusers"] = convert. toint32 (application ["allusers"]) + 1;
27 system. io. filestream fs = new system. io. filestream ("count.txt", system. io. filemode. openorcreate, system. io. fileaccess. readwrite );
28 system. io. streamwriter sw = new system. io. streamwriter (fs );
29 sw. writeline (application ["allusers"]);
30 sw. close ();
31 fs. close ();
32 application. unlock ();
33}
34
35 void session_end (object sender, eventargs e)
36 {
37 application. lock ();
38 application ["count"] = convert. toint32 (application ["count"])-1;
39 application. unlock ();
40
41}

Summary:
Applicationd is generally used to store various constants of variables that do not change frequently. For example, data information such as a system menu that is unlikely to change after a program is run.
Application has a good place. If it is not used properly, the stored data will not have timing. Because they coexist with applications.

Several common variables:
Application is an application-level variable.
Session is a user variable. When a user accesses multiple pages, data can be saved to another page, as long as the user is not logged out.
The viewstate page variable is equivalent to the global variable of the page. However, once you exit the current page, it will be lost.

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.