Brief Introduction to application and counter instances
Event-handling method description
Application_start () occurs when the application starts
This is the first time that he has received a request from any user.
Application_end () occurs when the application is closed, usually because the network server is restarting.
In application_beginrequest (), the Code on this page is executed when the application of each request obtains the code.
Application_endrequest ()
Simple Storage value instance
<% @ Page language = "vb" %>
<Script runat = "server">
Sub page_load (sender as object, e as eventargs)
Lbloutput. text = "page loading "&_
"Application started at:" & application ("time") & "<br> "&_
"Current time:" & datetime. now & "<br>"
End sub
Sub click (obj as object, e as eventargs)
Session. abandon ()
End sub
</Script>
<Html> <body>
<Form runat = "server">
<Asp Tutorial: label id = "lbloutput" runat = "server"/>
<Asp: button id = "btsubmit" runat = "server" onclick = "click" text = "end this session"/>
</Form>
</Body>
Simple counters using application
<% @ Page language = "c #" %>
<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en"
Http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd>
<Script runat = "server">
Void page_load ()
{
Lblsessioncount. text = application ["sessioncount"]. tostring ();
}
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">
<Title> show session count </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
Total application sessions:
<Asp: label
Id = "lblsessioncount"
Runat = "server"/>
</Div>
</Form>
</Body>
</Html>
Lock application
<% @ Page language = "c #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>
<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> untitled page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
The page has been requested
<Asp: label id = "mylabel" runat = "server"/>
Times!
</Div>
</Form>
</Body>
</Html>
File: default. aspx. cs
Using system;
Using system. data;
Using system. configuration;
Using system. web;
Using system. web. security;
Using system. web. ui;
Using system. web. ui. webcontrols;
Using system. web. ui. webcontrols. webparts;
Using system.web.ui.html controls;
Public partial class _ default: system. web. ui. page
{
Protected void page_load (object sender, eventargs e)
{
If (application ["pagecounter"]! = Null &&
(Int) application ["pagecounter"]> = 10)
{
Application. remove ("pagecounter ");
}
If (application ["pagecounter"] = null)
{
Application ["pagecounter"] = 1;
}
Else
{
Application. lock ();
Application ["pagecounter"] =
(Int) application ["pagecounter"] + 1;
Application. unlock ();
}
Mylabel. text = convert. tostring (application ["pagecounter"]);
}
}