Order of events called in a custom control

Discussion in 'ASP .Net Building Controls' started by Eddie Chan, Jan 25, 2006.

  1. Eddie Chan

    Eddie Chan Guest

    Dear Sir,

    I have a question on the order of event called during using a custom
    control when in
    1. not IsPostBack and 2. IsPostBack situation

    I have an aspx file with the following code.

    <%@ Page Language="C#" CodePage="65001" Debug="true"
    autoeventwireup="True" %>
    <%@ Register TagPrefix="GeneralDataGrid" Namespace="GeneralDataGrid "
    Assembly="GeneralDataGrid" %>

    <%@ import Namespace="System.Collections" %>
    <%@ Import Namespace="System.Data" %>


    <script runat="server">

    void Page_Init() {
    Response.Write("Start of Page Init<br>");
    Response.Write("End of Page Init<br>");
    }

    void Page_Load(Object sender, EventArgs e) {
    Response.Write("Start of Page Load<br>");
    Response.Write("End of Page Load<br>");
    }

    void Page_PreRender() {
    Response.Write("Start of Page PreRender<br>");
    Response.Write("End of Page PreRender<br>");
    }

    </script>

    <html>
    <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"
    bgcolor="#FFFFFF">
    <form runat="server">
    <GeneralDataGrid:CompositeDataGrid id="xxx" runat="server"/>
    <hr/>
    <p>
    <asp:Button id="btn" runat="server" Text="ginger"/>
    </form>
    </body>
    </html>

    and the following are the C# code for the custom control.

    // GeneralDataGrid.cs
    using System;

    namespace GeneralDataGrid {
    public class CompositeDataGrid : Control, INamingContainer {
    protected override void CreateChildControls() {
    Context.Response.Write("Start of CreateChildControls<br>");
    Context.Response.Write("End of CreateChildControls<br>");
    }
    protected override void OnLoad(EventArgs e) {
    Context.Response.Write("Start of OnLoad<br>");
    //------------------------------------------------
    // BLOCK A - Control "fsdkf;sk;flk" does not exist!
    //------------------------------------------------
    Control xxx = FindControl("fsdkf;sk;flk");
    //------------------------------------------------
    // END OF BLOCK A
    //------------------------------------------------
    Context.Response.Write("End of OnLoad<br>");
    }
    protected override void OnInit(EventArgs e) {
    Context.Response.Write("Start of OnInit<br>");
    Context.Response.Write("End of OnInit<br>");
    }
    protected override void OnPreRender(EventArgs e) {
    Context.Response.Write("Start of OnPreRender<br>");
    Context.Response.Write("End of OnPreRender<br>");
    }
    }
    }

    What I want to ask is if the line within BLOCK A is NOT remarked, then
    the result is as this
    (Not IsPostBack state)

    Start of OnInit
    End of OnInit
    Start of Page Init
    End of Page Init
    Start of Page Load
    End of Page Load
    Start of OnLoad
    Start of CreateChildControls
    End of CreateChildControls
    End of OnLoad
    Start of Page PreRender
    End of Page PreRender
    Start of OnPreRender
    End of OnPreRender

    You can see that CreateChildControls is called within OnLoad

    However, when I remark the the line between BLOCK A, the result is as
    follows.
    (Not IsPostBack state)

    Start of OnInit
    End of OnInit
    Start of Page Init
    End of Page Init
    Start of Page Load
    End of Page Load
    Start of OnLoad
    End of OnLoad
    Start of Page PreRender
    End of Page PreRender
    Start of CreateChildControls
    End of CreateChildControls
    Start of OnPreRender
    End of OnPreRender

    which CreateChildControls is called after Page_PreRender

    Why does it happen? Can somebody help?

    Eddie Chan
     
    Eddie Chan, Jan 25, 2006
    #1
    1. Advertising

  2. Eddie Chan

    Teemu Keiski Guest

    Hi,

    CreateChildControls runs when control / page is requested to create its set
    of child controls. It happens either when responding to a FindControl call,
    or accessing child controls say Controls property etc. CreateChildControls
    is called at PreRender stage (if not before) at the latest so that controls
    are there by the time Page is being rendered.

    Basically it goes so with postback data loading that in non-postback
    request, CreateChildControls is called at PreRender unless Controls are
    accessed before. But on postback, postback data is traversed near page_load
    (just before) to locate controls which caused the postback, which invokes
    FindControl call and again ensures that controls are created before Load
    stage.

    You can also verify that child controls are created at specific time by
    calling EnsureChildControls(); That's what FindControl and Controls accessor
    call before they return you the Control or ControlCollection instance

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke


    "Eddie Chan" <> wrote in message
    news:...
    > Dear Sir,
    >
    > I have a question on the order of event called during using a custom
    > control when in
    > 1. not IsPostBack and 2. IsPostBack situation
    >
    > I have an aspx file with the following code.
    >
    > <%@ Page Language="C#" CodePage="65001" Debug="true"
    > autoeventwireup="True" %>
    > <%@ Register TagPrefix="GeneralDataGrid" Namespace="GeneralDataGrid "
    > Assembly="GeneralDataGrid" %>
    >
    > <%@ import Namespace="System.Collections" %>
    > <%@ Import Namespace="System.Data" %>
    >
    >
    > <script runat="server">
    >
    > void Page_Init() {
    > Response.Write("Start of Page Init<br>");
    > Response.Write("End of Page Init<br>");
    > }
    >
    > void Page_Load(Object sender, EventArgs e) {
    > Response.Write("Start of Page Load<br>");
    > Response.Write("End of Page Load<br>");
    > }
    >
    > void Page_PreRender() {
    > Response.Write("Start of Page PreRender<br>");
    > Response.Write("End of Page PreRender<br>");
    > }
    >
    > </script>
    >
    > <html>
    > <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"
    > bgcolor="#FFFFFF">
    > <form runat="server">
    > <GeneralDataGrid:CompositeDataGrid id="xxx" runat="server"/>
    > <hr/>
    > <p>
    > <asp:Button id="btn" runat="server" Text="ginger"/>
    > </form>
    > </body>
    > </html>
    >
    > and the following are the C# code for the custom control.
    >
    > // GeneralDataGrid.cs
    > using System;
    >
    > namespace GeneralDataGrid {
    > public class CompositeDataGrid : Control, INamingContainer {
    > protected override void CreateChildControls() {
    > Context.Response.Write("Start of CreateChildControls<br>");
    > Context.Response.Write("End of CreateChildControls<br>");
    > }
    > protected override void OnLoad(EventArgs e) {
    > Context.Response.Write("Start of OnLoad<br>");
    > //------------------------------------------------
    > // BLOCK A - Control "fsdkf;sk;flk" does not exist!
    > //------------------------------------------------
    > Control xxx = FindControl("fsdkf;sk;flk");
    > //------------------------------------------------
    > // END OF BLOCK A
    > //------------------------------------------------
    > Context.Response.Write("End of OnLoad<br>");
    > }
    > protected override void OnInit(EventArgs e) {
    > Context.Response.Write("Start of OnInit<br>");
    > Context.Response.Write("End of OnInit<br>");
    > }
    > protected override void OnPreRender(EventArgs e) {
    > Context.Response.Write("Start of OnPreRender<br>");
    > Context.Response.Write("End of OnPreRender<br>");
    > }
    > }
    > }
    >
    > What I want to ask is if the line within BLOCK A is NOT remarked, then
    > the result is as this
    > (Not IsPostBack state)
    >
    > Start of OnInit
    > End of OnInit
    > Start of Page Init
    > End of Page Init
    > Start of Page Load
    > End of Page Load
    > Start of OnLoad
    > Start of CreateChildControls
    > End of CreateChildControls
    > End of OnLoad
    > Start of Page PreRender
    > End of Page PreRender
    > Start of OnPreRender
    > End of OnPreRender
    >
    > You can see that CreateChildControls is called within OnLoad
    >
    > However, when I remark the the line between BLOCK A, the result is as
    > follows.
    > (Not IsPostBack state)
    >
    > Start of OnInit
    > End of OnInit
    > Start of Page Init
    > End of Page Init
    > Start of Page Load
    > End of Page Load
    > Start of OnLoad
    > End of OnLoad
    > Start of Page PreRender
    > End of Page PreRender
    > Start of CreateChildControls
    > End of CreateChildControls
    > Start of OnPreRender
    > End of OnPreRender
    >
    > which CreateChildControls is called after Page_PreRender
    >
    > Why does it happen? Can somebody help?
    >
    > Eddie Chan
    >
     
    Teemu Keiski, Jan 28, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=

    order of events - dynamic checkbox events

    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=, Aug 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    595
    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=
    Aug 17, 2005
  2. Joe Iano

    user control and order of events in page

    Joe Iano, Jul 4, 2003, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    245
    Joe Iano
    Jul 7, 2003
  3. Matt Sokol
    Replies:
    2
    Views:
    426
    Theo Tillotson
    Aug 7, 2003
  4. cspoh
    Replies:
    0
    Views:
    267
    cspoh
    Jul 31, 2003
  5. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    419
    Chris
    Aug 30, 2005
Loading...

Share This Page