Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a web part in which i have called several protected overriden methods such as create child controls.

Looking on the net and throwugh various books OnInit should be fired first however in my case create child controls is the very first event that has been fired.

I'm guessing that my example is wrong but could anyone shed any light as to why the create child controls is being launched first?

        //}
    /// <summary>
    /// The CreateChildControlsEvent.
    /// </summary>
    protected override void CreateChildControls()
    {
        try
        {
            this.Controls.Add(new LiteralControl(_strCreateChildControlsFired));
        }
        catch (Exception exc)
        {
            this.Controls.Add(new LiteralControl("Booom.....CreateChildControls()" + exc.Message));
        }

    }
    /// <summary>
    /// The OnPreRenderEvent.
    /// </summary>
    /// <param name="e">(EventArgs)The default Event args parameter.</param>
    protected override void OnPreRender(EventArgs e)
    {
        try
        {
            this.Controls.Add(new LiteralControl(_strOnPreRenderFired));
            base.OnPreRender(e);
        }
        catch (Exception exc)
        {
            this.Controls.Add(new LiteralControl("Booom.....OnPreRender()" + exc.Message));
        }

    }
    ///// <summary>
    ///// The RenderWebPartEvent.
    ///// </summary>
    ///// <param name="output">(HtmlTextWriter) The default object that allows you to output to the screen in HTML format.</param>
    //protected override void RenderWebPart(HtmlTextWriter output)
    //{
    //    try
    //    {
    //        this.Controls.Add(new LiteralControl(_strRenderWebPartFired));
    //        base.RenderWebPart(output);
    //    }
    //    catch (Exception exc)
    //    {
    //        this.Controls.Add(new LiteralControl("Booom.....RenderWebPart()" + exc.Message));
    //    }

    //}
    /// <summary>
    /// The Render Evemt.
    /// </summary>
    /// <param name="writer">(HtmlTextWriter) The default text writer.</param>
    protected override void Render(HtmlTextWriter writer)
    {
        try
        {
            this.Controls.Add(new LiteralControl(_strRenderFired));
        }
        catch (Exception exc)
        {
            this.Controls.Add(new LiteralControl("Booom.....Render()" + exc.Message));
        }

    }
        /// <summary>
    /// The OnIt Event.
    /// </summary>
    /// <param name="e">(EventArgs)The default Event args parameter.</param>
    protected override void OnInit(EventArgs e)
    {
        try
        {
            this.Controls.Add(new LiteralControl(_strOnInitFired));
        }
        catch (Exception exc)
        {
            this.Controls.Add(new LiteralControl("Booom.....OnInit()" + exc.Message));
        }


    }
    /// <summary>
    /// The OnLoad Event.
    /// </summary>
    /// <param name="e">(EventArgs)The default Event args parameter.</param>
    protected override void OnLoad(EventArgs e)
    {
        try
        {
            this.Controls.Add(new LiteralControl(_strOnLoadFired));
        }
        catch (Exception exc)
        {
            this.Controls.Add(new LiteralControl("Booom.....OnLoad()" + exc.Message));
        }

    }

Sorry i have altered the code and some of the base. calls are missing.

share|improve this question
add comment

2 Answers

Wictor Wilén has a great blog post on this: SharePoint Web Part Event Flow in detail

I also suggest you read the MSDN documentation MSDN: ASP.NET Page Life Cycle Overview . A webpart is basically just a extended webcontrol, and the same rules apply here as for other ASP.NET webcontrols.

A nice guide which explains in details the order of events thrown can be found here: Complete Lifecycle of an ASP.Net page and controls

share|improve this answer
add comment

This image has been a lifesaver for me in the past:

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.