ASP.NET Events

Rédigé par Sozezzo - - Aucun commentaire

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.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        

        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

        

    </div>

    </form>

</body>

</html>

 


using System;

 

public partial class _Default : System.Web.UI.Page

{

    //  Event 1 - PreInit

    protected void Page_PreInit(object sender, EventArgs e)

    {

        /*

            1. Check the IsPostBack property to determine whether this is the first time the page is being processed.

            2. Create or re-create dynamic controls.

            3. Set a master page dynamically.

            4. Set the Theme property dynamically.

        

            Note: If the request is a postback, the values of the controls have not yet been restored from view state.

                  If you set a control property at this stage, its value might be overwritten in the next event.

         */

    }

 

    //  Event 2 - Init

    protected void Page_Init(object sender, EventArgs e)

    {

        /*

            1. This event fires after each control has been initialized.

            2. Each control's UniqueID is set and any skin settings have been applied.

            3. Use this event to read or initialize control properties.

            4. The “Init” 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. 

        */

    }

 

    //  Event 3 - InitComplete

    protected void Page_InitComplete(object sender, EventArgs e)

    {

        /*

          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.

          2. Raised by the  Page object.

          3. Use this event for processing tasks that require all initialization be complete.    

        */

    }

 

    //  Event 4 - OnPreLoad

    protected override void OnPreLoad(EventArgs e)

    {

        /*

          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.  

          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.

          3. Loads ViewState : ViewState data are loaded to controls.

          4. Loads Postback data : Postback data are now handed to the page controls.

        */

    }

 

    //  Event 5 - Load

    protected void Page_Load(object sender, EventArgs e)

    {

        /*

          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.

          2. This is the first place in the page lifecycle that all values are restored.

          3. Most code checks the value of IsPostBack to avoid unnecessarily resetting state.

          4. You may also call Validate and check the value of IsValid in this method.

          5. You can also create dynamic controls in this method.

          6. Use the OnLoad event method to set properties in controls and establish database connections.

        */

    }

 

 

    //  Event 6 - Control PostBack Event(s)

    protected void Button1_Click(object sender, EventArgs e)

    {

        /*

          1. ASP.NET now calls any events on the page or its controls that caused the PostBack to occur.

          2. Use these events to handle specific control events, such as a Button control's Click event or a TextBox control's TextChanged event.

          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.

          4. This is just an example of control event.. Here it is button click event that caused the postback.

        */

    }

 

    //  Event 7 - LoadComplete

    protected void Page_LoadComplete(object sender, EventArgs e)

    {

        /*

          1. Raised at the end of the event-handling stage.

          2. Use this event for tasks that require that all other controls on the page be loaded.

        */

    }

 

    //  Event 8 - OnPreRender

    protected override void OnPreRender(EventArgs e)

    {

        /*

          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.

          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.

          3. The PreRender event of individual controls occurs after the PreRender event of the page.

          4. Allows final changes to the page or its control.

          5. This event takes place before saving ViewState, so any changes made here are saved.

          6. For example : After this event, you cannot change any property of a button or change any viewstate value.

          7. Each data bound control whose DataSourceID property is set calls its DataBind method.

          8. Use the event to make final changes to the contents of the page or its controls.

        */

    }

 

 

    //  Event 9 - OnSaveStateComplete

    protected override void OnSaveStateComplete(EventArgs e)

    {

        /*

          1. Raised after view state and control state have been saved for the page and for all controls.

          2. Before this event occurs, ViewState has been saved for the page and for all controls.

          3. Any changes to the page or controls at this point will be ignored.

          1. Use this event perform tasks that require view state to be saved, but that do not make any changes to controls.

        */

    }

 

 

    // Method  10 - Render

    /*

      1. This is a method of the page object and its controls (and not an event).

      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.

    */

 

    //  Event 11 - UnLoad

    protected void Page_UnLoad(object sender, EventArgs e)

    {

        /*

          1. This event is used for cleanup code.

          2. At this point, all processing has occurred and it is safe to dispose of any remaining objects, including the Page object.

          3. Cleanup can be performed on-

             (a)Instances of classes i.e. objects

             (b)Closing opened files

             (c)Closing database connections.

          4. This event occurs for each control and then for the page.

          5. During the unload stage, the page and its controls have been rendered, so you cannot make further changes to the response stream.

          6. If you attempt to call a method such as the Response.Write method, the page will throw an exception.

        */

    }

}

 

Partial code test


    protected void Page_PreInit(object sender, EventArgs e)
    {
        Response.Write("<br/>" + "PreInit");
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        Response.Write("<br/>" + "Init");
    }

    protected void Page_InitComplete(object sender, EventArgs e)
    {
        Response.Write("<br/>" + "InitComplete");
    }

    protected override void OnPreLoad(EventArgs e)
    {
        Response.Write("<br/>" + "PreLoad");
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("<br/>" + "Load");
    }

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        Response.Write("<br/>" + "LoadComplete");
    }

    protected override void OnPreRender(EventArgs e)
    {
        Response.Write("<br/>" + "PreRender");
    }

    protected override void OnSaveStateComplete(EventArgs e)
    {
        Response.Write("<br/>" + "SaveStateComplete");
    }

    protected void Page_UnLoad(object sender, EventArgs e)
    {
        //Runtime Error : Response is not available in this context.
        //Response.Write("<br/>" + "UnLoad"); //Error
    }

Result:


PreInit
Init
InitComplete
PreLoad
Load
LoadComplete
PreRender
SaveStateComplete

 

 

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/

 

Les commentaires sont fermés.