ASP.NET Tutorial/Page Lifecycle/Page Events
Содержание
Error event example
<%@ Page Language="vb" %>
<html>
<head>
<title>Error event example</title>
<script runat="server">
Sub Page_Load()
Dim x, y, overflow As Integer
x = 1
y = 0
overflow = x / y
End Sub
Sub Page_Error()
Response.Write(Server.GetLastError.ToString())
Server.ClearError
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
Here is the sequence of events that are raised whenever you request a page:
1. PreInit
2. Init
3. InitComplete
4. PreLoad
5. Load
6. LoadComplete
7. PreRender
8. PreRenderComplete
9. SaveStateComplete
10. Unload
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Page Load";
}
void Button1_Click(object sender, EventArgs e)
{
Label1.Text += "<br />Button Click";
}
void Page_PreRender()
{
Label1.Text += "<br />Page PreRender";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Show Page Events</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button
id="Button1"
Text="Click Here"
OnClick="Button1_Click"
Runat="server" />
<br /><br />
<asp:Label
id="Label1"
Runat="server" />
</div>
</form>
</body>
</html>
Page events firing sequence (VB.net)
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub Page_Load()
Label1.Text = "Page Load"
End Sub
Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Label1.Text &= "<br />Button Click"
End Sub
Sub Page_PreRender()
Label1.Text &= "<br />Page PreRender"
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Show Page Events</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button
id="Button1"
Text="Click Here"
OnClick="Button1_Click"
Runat="server" />
<br /><br />
<asp:Label
id="Label1"
Runat="server" />
</div>
</form>
</body>
</html>
Page flow tracing
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="PageFlowTracing" Trace="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Page Flow</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="lblInfo"
runat="server" EnableViewState="False"/>
<asp:Button id="Button1"
runat="server"
Text="Button" OnClick="Button1_Click"/>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class PageFlowTracing : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
lblInfo.Text += "Page.Load event handled.<br/>";
if (Page.IsPostBack)
{
lblInfo.Text += "<b>This is the second time you"ve seen this page.</b><br/>";
}
}
private void Page_Init(object sender, System.EventArgs e)
{
lblInfo.Text += "Page.Init event handled.<br/>";
}
protected void Button1_Click(object sender, System.EventArgs e)
{
Trace.Write("Button1_Click", "About to update the label.");
lblInfo.Text += "Button1.Click event handled.";
Trace.Write("Button1_Click", "Label updated.");
}
private void Page_PreRender(object sender, System.EventArgs e)
{
lblInfo.Text += "Page.PreRender event handled.<br/>";
}
private void Page_Unload(object sender, System.EventArgs e)
{
lblInfo.Text += "Page.Unload event handled.<br/>";
}
}
Page life cycle events
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="PageFlow" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Page Flow</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="lblInfo" runat="server" EnableViewState="False">
</asp:Label>
<asp:Button id="Button1" runat="server"
Text="Button" OnClick="Button1_Click"></asp:Button>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class PageFlow : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
lblInfo.Text += "Page.Load event handled.<br/>";
if (Page.IsPostBack)
{
lblInfo.Text += "<b>second time</b><br/>";
}
}
private void Page_Init(object sender, System.EventArgs e)
{
lblInfo.Text += "Page.Init event handled.<br/>";
}
protected void Button1_Click(object sender, System.EventArgs e)
{
lblInfo.Text += "Button1.Click event handled.<br/>";
}
private void Page_PreRender(object sender, System.EventArgs e)
{
lblInfo.Text += "Page.PreRender event handled.<br/>";
}
private void Page_Unload(object sender, System.EventArgs e)
{
lblInfo.Text += "Page.Unload event handled.<br/>";
}
}
Unload event example
<%@ Page Language="vb" %>
<html>
<head>
<title>Unload event example</title>
<script runat="server">
Dim TheFile As System.IO.StreamReader
Sub Page_Load()
TheFile = System.IO.File.OpenText(MapPath("Init.aspx"))
Message.Text = "<pre>" & Server.HtmlEncode(TheFile.ReadToEnd()) & "</pre>"
End Sub
Sub Page_Unload()
TheFile.Close()
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>