Page lifecycle and dynamic controls

  • Thread starter Thread starter Hans Kesting
  • Start date Start date
H

Hans Kesting

Hi,

Is there good information about the asp.net page lifecycle in
combination with dynamically loaded controls? Or on "how to build
dynamic controls"? I keep hitting problems where values are not
available at the moment I need them.

Current problem:
In a dynamically loaded ascx there is a dropdown. Based on the selected
value (reloading an existing value or responding to a
SelectedIndexChanged event), I create a set of "property edit"
controls. But when I try to save all selected values, the dropdowns in
there (my dropdowns, based on existing dropdowns) sometimes come up
empty.
This happens especially the first time, so when the property controls
are built following a SelectedIndexChanged.

Any suggestions?

Hans Kesting
 
Hans Kesting said:
Hi,

Is there good information about the asp.net page lifecycle in combination
with dynamically loaded controls? Or on "how to build dynamic controls"? I
keep hitting problems where values are not available at the moment I need
them.

Current problem:
In a dynamically loaded ascx there is a dropdown. Based on the selected
value (reloading an existing value or responding to a SelectedIndexChanged
event), I create a set of "property edit" controls. But when I try to save
all selected values, the dropdowns in there (my dropdowns, based on
existing dropdowns) sometimes come up empty.
This happens especially the first time, so when the property controls are
built following a SelectedIndexChanged.

Any suggestions?

Hans Kesting

I don't know about you particular problem but you have to recreate everytime
your dynamic controls in your Page Init function. Still in the init function
you can set you control's content the first time the page loads
(!IsPostBack).

Subsequent page loads, the viewstate will kicks in but still, you have to
re-create you dynamic control every page loads (even on postbacks).

Hope it helps
 
Mike Gleason jr Couturier was thinking very hard :
I don't know about you particular problem but you have to recreate everytime
your dynamic controls in your Page Init function. Still in the init function
you can set you control's content the first time the page loads
(!IsPostBack).

Subsequent page loads, the viewstate will kicks in but still, you have to
re-create you dynamic control every page loads (even on postbacks).

Hope it helps

But if I should create everything in Page_Init, how can I respond to an
IndexChanged event, which doesn't happen until *after* Page_Load?
I know I should rebuild the controls every time. One of my problems is
that I the information I need to decide *what* to (re)build isn't
available yet at the time I should do it.

And an additional question: does Ajax change that lifecycle? (the
pulldown and the properties section ar in an UpdatePanel).

Hans Kesting
 
Hans Kesting said:
Mike Gleason jr Couturier was thinking very hard :

But if I should create everything in Page_Init, how can I respond to an
IndexChanged event, which doesn't happen until *after* Page_Load?
I know I should rebuild the controls every time. One of my problems is
that I the information I need to decide *what* to (re)build isn't
available yet at the time I should do it.

And an additional question: does Ajax change that lifecycle? (the pulldown
and the properties section ar in an UpdatePanel).

Hans Kesting

I see.. for the event part you can (after creating it dynamically):

MyDynamicControl.OnIndexChanged += new [Handler]

Mike
 
Mike Gleason jr Couturier presented the following explanation :
Hans Kesting said:
Mike Gleason jr Couturier was thinking very hard :

But if I should create everything in Page_Init, how can I respond to an
IndexChanged event, which doesn't happen until *after* Page_Load?
I know I should rebuild the controls every time. One of my problems is that
I the information I need to decide *what* to (re)build isn't available yet
at the time I should do it.

And an additional question: does Ajax change that lifecycle? (the pulldown
and the properties section ar in an UpdatePanel).

Hans Kesting

I see.. for the event part you can (after creating it dynamically):

MyDynamicControl.OnIndexChanged += new [Handler]

Mike

I know. The event does fire, my handler gets executed and adds the
'property' fields to the page, so the page looks OK. However when I
then hit the "save" button (after selecting values in the dropdowns of
some properties) those values are lost.
When I revisit the item, the dropdown is filled before the Load and the
property-section is created in the Load event. Now the values in the
property-pulldowns are saved correctly.

So how can I get the property-section to behave correctly after an
"index changed"?


Hans Kesting
 
Maybe Instead of adding dynamic controls in the event handler (selected
index changed), you can create your dynamic controls in the page Init
function when the selected index is discovered:

You can check if the selected index changed by looking at those in you page
Init:

HttpContext.Current.Request.Form["__EVENTTARGET"]
HttpContext.Current.Request.Form["__EVENTARGUMENT"]

Mike
 
Back
Top