PreInit

  • Vérifier s’il est la première fois que l’on appelle une page : IsPostBack
  • Créer ou recréer de contrôles dynamiques;
  • Configurer la page « Master »;
  • Configurer le thème dynamiquement;
  • Lire et écrire des values de propriétés;

S’il est un « PostBack »

  • Vérifier les valeurs des contrôles avant le « restore » du « ViewState »;
  • Récrire des valeurs de propriétés;

Init

  • L’événement qui est déclenché en premier;
  • Événement est utilisé pour initialiser les propriétés du contrôle;

**IniComplete **

  • Les changements faits sur le « ViewState »  sont persistés après le prochain « PostBack »;

PreLoad

  • L’événement qui fait le traitement  du « PostBack » et aussi du « Request ».

Load

  • Cet événement appelle la méthode « OnLoad »;
  • L’événement « Load » de la page est déclenché avant des contrôles individuels;

ControlEvents

  • Cet événement est utilisé avec des spécifiques évents de contrôles comme « Button » avec l’événement « Click », ou le contrôle « TextBox »avec l’événement « TextChanged »;
  • Dans le cas d’un « PostBack », et si la page contient  validation de contrôles, le « Page.IsValid » et les validations de chaque contrôle sont déclenchés;

LoadComplete

  • Cet événement est déclenché après tout « PostBack »and « View-State » sont chargés, et après la méthode « OnLoad » des tous les contrôles ont été déclenchés;

PreRender

  • Cet événement est pour effectuer des changements sur un contrôle avant qu’il soit sur la page, mais ces modifications ne sont pas sauvegardées. On peut l’utiliser pour associer un « DataSourceId » à un « DataBind »;

PreRenderComplete

  • Cet événement est déclenché après l’événement « PreRender » est fini;

SaveStateComplete

  • Cet événement est déclenché après le « View-State » a été sauvegardé de tous les contrôles et la page;
  • Tout le changement ne sont plus sauvegardé;

RenderComplete

  • C’est le dernier événement déclenché avant d’affichage de la page;

Unload

  • Cet événement est déclenché quand le contrôle est déchargé de la mémoire;
  • Il est utilisé pour finaliser des connexions, fermer des fichiers, effacer de données temporaires.
 1
 2<@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" >
 3
 4 
 5
 6<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">;
 7
 8 
 9
10<html xmlns="http://www.w3.org/1999/xhtml">;
11
12<head runat="server">;
13
14    <title>;Untitled Page</title>;
15
16</head>;
17
18<body>;
19
20    <form id="form1" runat="server">;
21
22    <div>;
23
24        
25
26        <asp:Label ID="Label1" runat="server" Text="Label">;</asp:Label>;
27
28        
29
30    </div>;
31
32    </form>;
33
34</body>;
35
36</html>;
  1
  2using System;
  3
  4public partial class _Default : System.Web.UI.Page
  5
  6{
  7
  8    //  Event 1 - PreInit
  9
 10    protected void Page_PreInit(object sender, EventArgs e)
 11
 12    {
 13
 14        /*
 15
 16            1. Check the IsPostBack property to determine whether this is the first time the page is being processed.
 17
 18            2. Create or re-create dynamic controls.
 19
 20            3. Set a master page dynamically.
 21
 22            4. Set the Theme property dynamically.
 23
 24            Note: If the request is a postback, the values of the controls have not yet been restored from view state.
 25
 26                  If you set a control property at this stage, its value might be overwritten in the next event.
 27
 28         */
 29
 30    }
 31
 32    //  Event 2 - Init
 33
 34    protected void Page_Init(object sender, EventArgs e)
 35
 36    {
 37
 38        /*
 39
 40            1. This event fires after each control has been initialized.
 41
 42            2. Each control's UniqueID is set and any skin settings have been applied.
 43
 44            3. Use this event to read or initialize control properties.
 45
 46            4. The &ldquo;Init&rdquo; event is fired first for the most bottom control in the hierarchy, and then fired up the hierarchy until it is fired for the page itself.
 47
 48        */
 49
 50    }
 51
 52    //  Event 3 - InitComplete
 53
 54    protected void Page_InitComplete(object sender, EventArgs e)
 55
 56    {
 57
 58        /*
 59
 60          1. Till now the viewstate values are not yet loaded, hence you can use this event to make changes to view state that you want to make sure are persisted after the next postback.
 61
 62          2. Raised by the  Page object.
 63
 64          3. Use this event for processing tasks that require all initialization be complete.
 65
 66        */
 67
 68    }
 69
 70    //  Event 4 - OnPreLoad
 71
 72    protected override void OnPreLoad(EventArgs e)
 73
 74    {
 75
 76        /*
 77
 78          1. Raised after the page loads view state for itself and all controls, and after it processes postback data that is included with the Request instance.
 79
 80          2. Before the Page instance raises this event, it loads view state for itself and all controls, and then processes any postback data included with the Request instance.
 81
 82          3. Loads ViewState : ViewState data are loaded to controls.
 83
 84          4. Loads Postback data : Postback data are now handed to the page controls.
 85
 86        */
 87
 88    }
 89
 90    //  Event 5 - Load
 91
 92    protected void Page_Load(object sender, EventArgs e)
 93
 94    {
 95
 96        /*
 97
 98          1. The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page.
 99
100          2. This is the first place in the page lifecycle that all values are restored.
101
102          3. Most code checks the value of IsPostBack to avoid unnecessarily resetting state.
103
104          4. You may also call Validate and check the value of IsValid in this method.
105
106          5. You can also create dynamic controls in this method.
107
108          6. Use the OnLoad event method to set properties in controls and establish database connections.
109
110        */
111
112    }
113
114    //  Event 6 - Control PostBack Event(s)
115
116    protected void Button1_Click(object sender, EventArgs e)
117
118    {
119
120        /*
121
122          1. ASP.NET now calls any events on the page or its controls that caused the PostBack to occur.
123
124          2. Use these events to handle specific control events, such as a Button control's Click event or a TextBox control's TextChanged event.
125
126          3. In a postback request, if the page contains validator controls, check the IsValid property of the Page and of individual validation controls before performing any processing.
127
128          4. This is just an example of control event.. Here it is button click event that caused the postback.
129
130        */
131
132    }
133
134    //  Event 7 - LoadComplete
135
136    protected void Page_LoadComplete(object sender, EventArgs e)
137
138    {
139
140        /*
141
142          1. Raised at the end of the event-handling stage.
143
144          2. Use this event for tasks that require that all other controls on the page be loaded.
145
146        */
147
148    }
149
150    //  Event 8 - OnPreRender
151
152    protected override void OnPreRender(EventArgs e)
153
154    {
155
156        /*
157
158          1. Raised after the Page object has created all controls that are required in order to render the page, including child controls of composite controls.
159
160          2. The Page object raises the PreRender event on the Page object, and then recursively does the same for each child control. The PreRender event of individual controls occurs after the PreRender event of the page.
161
162          3. The PreRender event of individual controls occurs after the PreRender event of the page.
163
164          4. Allows final changes to the page or its control.
165
166          5. This event takes place before saving ViewState, so any changes made here are saved.
167
168          6. For example : After this event, you cannot change any property of a button or change any viewstate value.
169
170          7. Each data bound control whose DataSourceID property is set calls its DataBind method.
171
172          8. Use the event to make final changes to the contents of the page or its controls.
173
174        */
175
176    }
177
178    //  Event 9 - OnSaveStateComplete
179
180    protected override void OnSaveStateComplete(EventArgs e)
181
182    {
183
184        /*
185
186          1. Raised after view state and control state have been saved for the page and for all controls.
187
188          2. Before this event occurs, ViewState has been saved for the page and for all controls.
189
190          3. Any changes to the page or controls at this point will be ignored.
191
192          1. Use this event perform tasks that require view state to be saved, but that do not make any changes to controls.
193
194        */
195
196    }
197
198    // Method  10 - Render
199
200    /*
201
202      1. This is a method of the page object and its controls (and not an event).
203
204      2. The Render method generates the client-side HTML, Dynamic Hypertext Markup Language (DHTML), and script that are necessary to properly display a control at the browser.
205
206    */
207
208    //  Event 11 - UnLoad
209
210    protected void Page_UnLoad(object sender, EventArgs e)
211
212    {
213
214        /*
215
216          1. This event is used for cleanup code.
217
218          2. At this point, all processing has occurred and it is safe to dispose of any remaining objects, including the Page object.
219
220          3. Cleanup can be performed on-
221
222             (a)Instances of classes i.e. objects
223
224             (b)Closing opened files
225
226             (c)Closing database connections.
227
228          4. This event occurs for each control and then for the page.
229
230          5. During the unload stage, the page and its controls have been rendered, so you cannot make further changes to the response stream.
231
232          6. If you attempt to call a method such as the Response.Write method, the page will throw an exception.
233
234        */
235
236    }
237
238}

Partial code test

 1
 2    protected void Page_PreInit(object sender, EventArgs e)
 3    {
 4        Response.Write("<br/>" + "PreInit");
 5    }
 6
 7    protected void Page_Init(object sender, EventArgs e)
 8    {
 9        Response.Write("<br/>" + "Init");
10    }
11
12    protected void Page_InitComplete(object sender, EventArgs e)
13    {
14        Response.Write("<br/>" + "InitComplete");
15    }
16
17    protected override void OnPreLoad(EventArgs e)
18    {
19        Response.Write("<br/>" + "PreLoad");
20    }
21
22    protected void Page_Load(object sender, EventArgs e)
23    {
24        Response.Write("<br/>" + "Load");
25    }
26
27    protected void Page_LoadComplete(object sender, EventArgs e)
28    {
29        Response.Write("<br/>" + "LoadComplete");
30    }
31
32    protected override void OnPreRender(EventArgs e)
33    {
34        Response.Write("<br/>" + "PreRender");
35    }
36
37    protected override void OnSaveStateComplete(EventArgs e)
38    {
39        Response.Write("<br/>" + "SaveStateComplete");
40    }
41
42    protected void Page_UnLoad(object sender, EventArgs e)
43    {
44        //Runtime Error : Response is not available in this context.
45        //Response.Write("<br/>" + "UnLoad"); //Error
46    }

Result:

 1
 2
 3PreInit
 4Init
 5InitComplete
 6PreLoad
 7Load
 8LoadComplete
 9PreRender
10SaveStateComplete

source :

ASP.NET Page Life Cycle Overview : https://msdn.microsoft.com/en-us/library/ms178472.aspx

http://www.codeproject.com/Tips/444310/ASP-NET-Page-Life-Cycle-Events

http://www.c-sharpcorner.com/UploadFile/8911c4/page-life-cycle-with-examples-in-Asp-Net/