程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> asp.net計算網站訪問量常用代碼

asp.net計算網站訪問量常用代碼

編輯:關於ASP.NET

      前台代碼:  
    <%@ 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)  
        { 
            //  在應用程序關閉時運行的代碼 
            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)  
        {  
            // 在出現未處理的錯誤時運行的代碼 
        } 
        void Session_Start(object sender, EventArgs e)  
        { 
            // 在新會話啟動時運行的代碼 
            Application.Lock(); 
            int Oncount = 0; 
            Oncount =(int) Application["onlinecount"]; 
            Oncount += 1; 
            object Onobj = Oncount; 
            Application["onlinecount"] = Onobj; 
             
            //將數據記錄回到文件中 
            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)  
        { 
            // 在會話結束時運行的代碼。  
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式設置為 
            // InProc 時,才會引發 Session_End 事件。如果會話模式設置為 StateServer 
            // 或 SQLServer,則不會引發該事件。 
        } 
    </script> 

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved