Front-end code:
<% @ 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> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Table style = "width: 269px; height: 75px">
<Tr>
<Td align = "center" style = "font-weight: bold;
Font-size: 30px; width: 237px; color: lime; background-color: gray; ">
Website access volume </td>
</Tr>
<Tr>
<Td align = "center" style = "width: 237px; background-color: gray;">
<Asp: Label ID = "onlineCount" runat = "server" T
Ext = "" Width = "62px"> <% = Application ["onlinecount"] %>
</Asp: Label> visitor
</Td>
</Tr>
</Table>
</Div>
</Form>
</Body>
</Html>
Global. asax code:
<% @ Application Language = "C #" %>
<% @ Import Namespace = "System. IO" %>
<Script runat = "server">
Void Application_Start (object sender, EventArgs e)
{
// Code that runs when the application starts
Int count = 0;
StreamReader sdr;
// Obtain the file path
String filePath = Server. MapPath ("count.txt ");
// Open the file
Sdr = File. OpenText (filePath );
// Read the file
While (sdr. Peek ()! =-1)
{
String str = sdr. ReadLine ();
// Convert the mandatory string type into integer data
Count = int. Parse (str );
}
Sdr. Close ();
Object objcount = count;
Application ["onlinecount"] = count;
}
Void Application_End (object sender, EventArgs e)
{
// Code that runs when the application is closed
Int Oncount = 0;
Oncount = (int) Application ["onlinecount"];
String filepath = Server. MapPath ("count ");
StreamWriter swr = new StreamWriter (filepath, false );
Swr. WriteLine (Oncount );
Swr. Close ();
}
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 ();
Int Oncount = 0;
Oncount = (int) Application ["onlinecount"];
Oncount + = 1;
Object Onobj = Oncount;
Application ["onlinecount"] = Onobj;
// Return the data record to the file
String filepath = Server. MapPath ("count.txt ");
StreamWriter swr = new StreamWriter (filepath, false );
Swr. WriteLine (Oncount );
Swr. Close ();
Application. UnLock ();
}
Void Session_End (object sender, EventArgs e)
{
// The code that runs when the session ends.
// Note: Only the sessionstate mode in the Web. config file is set
// The Session_End event is triggered only when InProc is used. If the session mode is set to StateServer
// Or SQLServer, the event is not triggered.
}
</Script>