您的位置:首页 > 编程语言 > ASP

asp.net页面生命周期 各时期事件

2009-11-17 10:25 302 查看
using System;
using System.Collections.Specialized;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace WebApplication1
{
public partial class WebForm4 : System.Web.UI.Page
{

private const string INFO_DIR = @"c:/Downloads/";
public static int requestCount;

/// <summary>
///典型应用:使用IsPostBack属性确定是否是第一次处理该页;
///创建或者重新创建动态控件;
///动态设置母版页;
////动态设置Theme属性;
///读取或者设置配置文件属性值。
///注意:如果请求是回发请求,则控件的值尚未从视图状态还原。如果在此阶段设置控件属性,则值可能会在下一阶段被改写。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_PreInit(object sender, EventArgs e)
{

}
/// <summary>
/// 在所有控件都已初始化且已应用所有外观设置后引发。使用该事件来读取或初始化控件属性
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("Page_Init<br/>");

}
/// <summary>
/// 由 Page 对象引发。使用该事件来处理要求先完成所有初始化工作的任务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_InitComplete(object sender, EventArgs e)
{
Response.Write("Page_InitComplete<br/>");

}
/// <summary>
/// 如果需要在 Load 事件之前对页或控件执行处理,请使用该事件。
///在 Page 引发该事件后,它会为自身和所有控件加载视图状态,然后会处理 Request 实例包括的任何回发数据。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_PreLoad(object sender, EventArgs e)
{
Response.Write("Page_PreLoad<br/>");

}
/// <summary>
/// Page 在 Page 上调用 OnLoad 事件方法,然后以递归方式对每个子控件执行相同操作,如此循环往复,直到加载完本页和所有控件为止。
///使用 OnLoad 事件方法来设置控件中的属性并建立数据库连接。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Page_Load<br/>");

}
/// <summary>
/// 对需要加载页上的所有其他控件的任务使用该事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_LoadComplete(object sender, EventArgs e)
{
// Create a variable to use when iterating
// through the UserLanguages property.
int langCount;
int requestNumber = Interlocked.Increment(ref requestCount);
// Create the file to contain information about the request.
string strFilePath = INFO_DIR + requestNumber.ToString() + @".txt";
StreamWriter sw = File.CreateText(strFilePath);
try
{
foreach (string s in Request.Form)
{
sw.WriteLine("Form: " + Server.HtmlEncode(s));
}
if (IsPostBack)
{
sw.WriteLine("******************IsPostBack");
}
else
{
sw.WriteLine("******************");
}

NameValueCollection whc = Request.Headers;
for (int i = 0; i < whc.Count; i++)
{
sw.WriteLine(whc.GetKey(i) + ":" + whc[i]);
}
if (IsPostBack)
{
sw.WriteLine("******************IsPostBack");
}
else
{
sw.WriteLine("******************");
}
sw.WriteLine(Request.HttpMethod);
foreach (string c in Request.AcceptTypes)
{
sw.WriteLine(c + "1");
}

}

finally
{
// Close the stream to the file.
sw.Close();
}

lblInfoSent.Text = "Information about this request has been sent to a file.";

}
/// <summary>
/// 在该事件发生前:
/// Page 对象会针对每个控件和页调用 EnsureChildControls。
/// 设置了 DataSourceID 属性的每个数据绑定控件会调用 DataBind 方法。有关更多信息,请参见下面的数据绑定控件的数据绑定事件。
/// 页上的每个控件都会发生 PreRender 事件。使用该事件对页或其控件的内容进行最后更改。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_PreRender(object sender, EventArgs e)
{
Response.Write("Page_PreRender<br/>");

}
/// <summary>
/// 在该事件发生前,已针对页和所有控件保存了 ViewState。将忽略此时对页或控件进行的任何更改。
///使用该事件执行满足以下条件的任务:要求已经保存了视图状态,但未对控件进行任何更改。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_SaveStateComplete(object sender, EventArgs e)
{
Response.Write("Page_SaveStateComplete<br/>");

}

/// <summary>
///该事件首先针对每个控件发生,继而针对该页发生。在控件中,使用该事件对特定控件执行最后清理,如关闭控件特定数据库连接。
///对于页自身,使用该事件来执行最后清理工作,如:关闭打开的文件和数据库连接,或完成日志记录或其他请求特定任务。
///注意
///在卸载阶段,页及其控件已被呈现,因此无法对响应流做进一步更改。如果尝试调用方法(如 Response.Write 方法),则该页将引发异常
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Unload(object sender, EventArgs e)
{
int i = 0;
i++;//这行代码是用来设置断点的,为什么不用Response.Write? *****

}

protected void Button1_Click1(object sender, EventArgs e)
{
Label1.Text = "ControlEvent";
Response.Write("Button事件触发!<br/>");
}

protected void Button2_Click(object sender, EventArgs e)
{
lblInfoSent.Text = "Hello, " + Server.HtmlEncode(txtBoxName.Text) +
".
4000
You have created a new request info file.";
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息