Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Order of events called in a custom control

Reply
Thread Tools

Order of events called in a custom control

 
 
Eddie Chan
Guest
Posts: n/a
 
      01-25-2006
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

 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      01-28-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Events Events Events Please Help Chris ASP .Net Web Controls 0 08-30-2005 08:21 PM
order of events - dynamic checkbox events =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?= ASP .Net 0 08-17-2005 10:36 PM
ControlDesigner not invoked on custom control when control is rendered within another custom control Matt Sokol ASP .Net Building Controls 2 08-07-2003 07:13 AM
How to Display DropDownList with preserved order (custom order) cspoh ASP .Net Web Controls 0 07-31-2003 09:19 AM
user control and order of events in page Joe Iano ASP .Net Building Controls 2 07-07-2003 02:13 AM



Advertisments