您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> asp.net编程 >> 正文

asp.net计算网站访问量常用代码

作者:佚名    责任编辑:admin    更新时间:2022-06-22

  前台代码:  
<%@ 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>无标题页</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;"> 
                    网站访问量</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>位访问者 
                </td> 
            </tr> 
        </table> 
   
    </div> 
    </form> 
</body> 
</html>  

Global.asax代码: 
<%@ Application Language="C#" %> 
<%@ Import Namespace="System.IO"  %> 
<script runat="server"> 
     
    void Application_Start(object sender, EventArgs e)  
    { 
        // 在应用程序启动时运行的代码 
        int count = 0; 
        StreamReader sdr; 
        // 获取文件路径 
        string filePath = Server.MapPath("count.txt"); 
        // 打开文件 
        sdr = File.OpenText(filePath); 
        // 读取文件 
        while(sdr.Peek()!=-1) 
        { 
            string str = sdr.ReadLine(); 
            // 把字符串强制类型转换成整型数据 
            count = int.Parse(str); 
        } 
        sdr.Close(); 
        object objcount = count; 
        Application["onlinecount"] = count; 
    } 
     
    void Application_End(object sender, EventArgs e)  
    {