Dynamically Loaded User Control Event Wiring

A

Andrew Robinson

I have a page that can load a number of different user controls. Each of
these user controls inherits from a common base class and the controls are
loaded based on application state, status, etc and the specific type of
control frequently changes. I have a common event in the base class that
each child user control inherits from and that the page wires up and uses to
get status from the child user control. I hope all pretty simple and not too
far out there....

I am loading and wiring the control in the Page_Load event or during a
secondary event in reponse to a button click on the page (in the page
class). The user control is added to a place holder control on the page.

This all works fine but somehow I am not receiving the initial invocation on
of the event on the child user control. It does work the second time around.

Bottom line: during which event on a page should I be loading and wiring
events on a dynamically loaded user control?

Any help on this would be appriciated.

-Andy
 
G

Guest

Andrew Robinson said:
I have a page that can load a number of different user controls. Each of
these user controls inherits from a common base class and the controls are
loaded based on application state, status, etc and the specific type of
control frequently changes. I have a common event in the base class that
each child user control inherits from and that the page wires up and uses
to get status from the child user control. I hope all pretty simple and not
too far out there....

I am loading and wiring the control in the Page_Load event or during a
secondary event in reponse to a button click on the page (in the page
class). The user control is added to a place holder control on the page.

This all works fine but somehow I am not receiving the initial invocation
on of the event on the child user control. It does work the second time
around.

Bottom line: during which event on a page should I be loading and wiring
events on a dynamically loaded user control?

The best place to load controls is in the Page_Init() method, I think.
 
B

bruce barker

if you dynamically add a control, on the next postback and all
subsequent postbacks) you should re-add the control and wireup events in
the oninit event.

-- bruce (sqlwork.com)
 
A

Andrew Robinson

Ok, one more piece of missing info: The particular flavor of my dynamic user
control is determined by info that is stored in ViewState on the host page
(which is not loaded until Page_Init). Seems like a chicken and egg issue.

Thanks,
 
S

Steven Cheng[MSFT]

Hi Andrew,

As for dynamic created control, they need to be added into Page's control
hierarchy(and wireup event handler) in either Page's Init or Load event.
Also, for ViewState, it is loading after Init and Before Load event. Thus,
if you have some dynamic control generation code logic that depend on
variables in ViewState, you need to use Load event instead of Init event.

One way to avoid using viewstate and also use Init for conditional dynamic
control generation is use a hidden field on page to store the certain
variable that control the dynamic control generation. Such hidden field can
be access through Request.Form collection directly in Init event(before
viewstate load).

Anyway, if possible, you can try show us a simplified page that can repro
the problem you met so that we can have a further look into it.


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

Hi Andrew,

Have you got any further idea on this issue or does the suggestion in
previous reply help you a little?
Please feel free to post here if there is anything else we can help.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top