Asp.net common website access count code

Source: Internet
Author: User

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>

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.