62,074
社区成员
发帖
与我相关
我的任务
分享
namespace Web
{ public class BasePage : System.Web.UI.Page
{
private long ProgramBeginRunTime;
private long programRunTime;
public BasePage()
{
this.ProgramBeginRunTime = System.Environment.TickCount; //程序开始运行时间
}
/// <summary>
/// 填充页面上显示程序运行时间的文本控件
/// </summary>
/// <param name="literal">显示程序运行时间的文本控件</param>
private void ProgramRunTime()
{
long ProgramEndRunTime = System.Environment.TickCount;
programRunTime = ProgramEndRunTime - this.ProgramBeginRunTime;
System.Web.UI.LiteralControl literal = new System.Web.UI.LiteralControl( "<!-- 程序运行时间 " + programRunTime.ToString() + " 毫秒 -->" );
this.Page.Controls.Add( literal ); //添加到页面末尾
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
this.ProgramRunTime();
base.Render (writer);
}}
}
Private void Page_Load(object sender,EventArgs e)
{
DateTime begin=System.DateTime.Now;
...... //中間是許多的頁面載入時要完成的功能
DateTime end=System.DateTime.Now;
//最後你將begin和end這兩個時間的差值得出來,我想這個應該可以說是頁面執行時間吧......
}
protected void Application_BeginRequest(Object sender, EventArgs e)
{
Application["StartTime"] = System.DateTime.Now;
}
protected void Application_EndRequest(Object sender, EventArgs e)
{
System.DateTime startTime = (System.DateTime)Application["StartTime"];
System.DateTime endTime = System.DateTime.Now;
System.TimeSpan ts = endTime - startTime;
Response.Write("页面执行时间:"+ ts.Milliseconds +" 毫秒");
}