Why CreateChildControls method is invoking between Begin ProcessPostData and End ProcessPostData

U

Umut Tezduyar

If I create and add a control that implemets IPostBackDataHandler on the
CreateChildControl phase, than in the postback CreateChildControl pahse is
invoked at the Load Postback Data phase. But this logic is not suitable for
me. I want my control invoke its CreateChildControl method every time, befor
the PreRender method not on the Load Postback Dataphase. Is there a way for
this.

Ex:

1) Add a System.Web.UI.WebControls.Textbox txt to the CONTROL1 at the
CreateChildControl.
2)Add a System.We.UI.LinkButton btn to the CONTROL1 at the
CreateChildControl.
3)Page is displayed
4)LinkButton is clicked
5)CreateChildControl method of the CONTROL1 is invoked between the Begin
ProcessPostData and End ProcessPostData
 
S

Steven Cheng[MSFT]

Hi Umut,

Thanks for your posting. As for the problem you mentioned, here are some of
my understandings:

As for the asp.net server control, (composite control), it'll call
createchildcontrol when needed, not always before or after a certain event.
Generally the behavior is like below:
1. IF the control is first time loaded( not post back), the
CreateChildControls may be called before PreRender and after Load. Also, we
can use EnsureChildControl method to force it be executed or manually call
it.

2. In Post Back scenario, since the Control's child controls need to be
constructed before the ViewState is loaded (or maybe PostData is retrieved)
so that the control can reretrive it's properties from ViewState or update
them via the new posted Data in the request's Form collection.
However, as I mentoined above, we can manually call the
CreateChildControls() method later. For example, we need to create a new
dynamic controls via a certain Flag member variable which is set in the
postback event. Then, we can use the following code in the post back event
handler

handler (object source, EventArgs e)
{
this.FlagMember = XXX;

if(this.ChildControlsCreated)
{
this.ChildControlsCreated = false;
this.CreateChildControls();
}
}

In another word, we can Recreate the control tree when needed , but can not
change the control's default behavior such as create control tree before
LoadViewState when post back.
If you have anything unclear, please free to post here. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
U

Umut Tezduyar

Thans your detailed explanation. You have answered my similar question
previously. That was really helpfull for me. :)
Calling the CreateChildControls method every time is before rendering is
consuming resources.
Is there a way to understand if it was called before load state.

While i was debuging controls, i saw an enum, that defines the Controls
state. But it is private.
 
S

Steven Cheng[MSFT]

Hi Umut,

Thanks for your followup and the further description on your concerns. As
for the problems you mentioned, here are my suggestions:

1.Is there a way to understand if it was called before load state.
=======================================================
No such a property or method directly. However, as I mentioned in the
former message, the "CreateChildControls" will apparently be called before
LoadViewState at "Postback" requests, so that means the first time
"CreatChildControls" is called(when postback), it is certainly before load
viewstate( if we don't disable viewstate:)). Also, you can check the
"ChildControlsCreated" property to see whether the "CreateChildControls"
has been called before.


2.Calling the CreateChildControls method every time is before rendering is
consuming resources.
=============================================
Yes, completely recreating the Control tree will have performance concerns.
However, we can avoid it via only update part of the control tree via the
following means;

We can determine what sub controls need to update according on the new post
back datas(or events). Then, we need to declare some class members for
these sub controls, so that later in the post back event, when we need to
adjust the control tree, we can just reference those sub controls via the
class member variables and make modification on them instead of recreating
the whole control tree. For example:

class MyControl: WebControl
{
private Label lbl;
private TextBox txt;

CreateChildControls
{
lbl = new Label();
.. ....

txt = new TextBox();
...........

}


.. postback event handler(..)
{
if(need adjust control tree)
{
UpdatePartControls();

}

}


UpdatePartControls()
{
//modify those sub controls via class member reference
txt......
lbl ....

}

}


Just some of my understanding. If you have anyother questions or ideas,
please feel free to post here.
Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top