Order of events called in a custom control

E

Eddie Chan

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
 
T

Teemu Keiski

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top