private void Page_Load(object sender, System.EventArgs e)


...{

this.lbltotol.Text=Application["totol"].ToString(); //把totol的值賦給lbltotol並顯示出來

this.lblonline.Text=Application["online"].ToString();//把online當前在線的值賦給lblonline並顯示出來


// 在此處放置用戶代碼以初始化頁面

}

用ASP.Net統計 網站流量代碼
在global.asax.as窗口中:

using System;

using System.Collections;

using System.ComponentModel;

using System.Web;

using System.Web.SessionState;

using System.Data.SqlClIEnt;

namespace login


...{


/**//// <summary>

/// Global 的摘要說明。

/// </summary>

public class Global : System.Web.HttpApplication

...{


/**//// <summary>

/// 必需的設計器變量。

/// </summary>

private System.ComponentModel.IContainer components = null;


public Global()


...{

InitializeComponent();

}

protected void Application_Start(Object sender, EventArgs e)//開始


...{//連接數據庫

SqlConnection con=new SqlConnection("server=(local);database=test;uid=sa;pwd=;");

con.Open();

SqlCommand cmd=new SqlCommand("select *from count",con);

int count=Convert.ToInt32(cmd.ExecuteScalar());//返回首行首列

&nbs

Application.Add("totol",count);//賦初值


Application.Add("online",0);


}

protected void Session_Start(Object sender, EventArgs e)


...{ // Session.Timeout=1; //設定過期時間 一般不用設置

Application.Lock();//關閉會話 解決同時發生的情況

Application["totol"]=(int)Application["totol"]+1;//總的訪問量加一

Application["online"]=(int)Application["online"]+1;//當前在線人數加一

Application.UnLock();

}


protected void Application_BeginRequest(Object sender, EventArgs e)


...{


}


protected void Application_EndRequest(Object sender, EventArgs e)


...{


}


protected void Application_AuthenticateRequest(Object sender, EventArgs e)


...{


}


protected void Application_Error(Object sender, EventArgs e)


...{


}


protected void Session_End(Object sender, EventArgs e)//對話結束


...{ Application.Lock();//鎖

Application["online"]=(int)Application["online"]-1;//當前在線人數在關閉會話是減一

Application.UnLock();

}


protected void Application_End(Object sender, EventArgs e)


...{ //把訪問量更新到數據庫的歷史訪問量的值

SqlConnection con=new SqlConnection("server=(local);database=test;uid=sa;pwd=;");

con.Open();

SqlCommand cmd=new SqlCommand("update count set num="+Application["totol"].ToString(),con);//把當前totol 的值更新到數據庫上

cmd.ExecuteNonQuery();//開始執行更新

con.Close();//關閉


}


Web 窗體設計器生成的代碼#region Web 窗體設計器生成的代碼


/**//// <summary>

/// 設計器支持所需的方法 - 不要使用代碼編輯器修改

/// 此方法的內容。

/// </summary>

private void InitializeComponent()

...{

this.components = new System.ComponentModel.Container();

}

#endregion

}

}
顯示流量頁面::