Loading User controls

J

John

Hi all,

I'm having a little problem understanding the concepts of dynamically
loading/unloading user conrols:

1. If I have a couple of usercontrols embedded within a few tables cells on
my page, setting the usercontrol's visible to false still fires the
Page_Load event. Isn't this unnecessary overhead when it doesn't even show
in my page.

2. For my usercontrols to be able to see one another's methods, do I have to
define the usercontrol within the code-behind of each usercontrol I wish to
reference?

3. To dynamically load/unload usercontrols, must I register them at least
within my page at design-time? This seems 'un-dynamic' to me.

4. What method do I use to run code the first time my user control is
dynamically loaded. I don't want to run the code again after that unless it
is removed then re-loaded again.


There clearly is much for me to learn about usercontrols. If the above four
questions could please be answered and perhaps a pointer to some good online
reading material, I really would appreciate this!

Thanks.

Regards
John.
 
M

Marina

1. Loading a control and making it not visible are 2 different things.
Invisible controls will not be rendered to the page - but they are still
created, and their page_load will still fire.

2. You should define public methods to allow this. You will also need to
have a way for one control to get a reference to the instance of the other
control.

3. To dynamically load controls, you do not need to register anything with
the page. You can just call the LoadControl method of the page, and then add
the newly created usercontrol to your page.
 
S

Sreejumon[MVP]

Hi John,

Here is the answer to your queries.
1. If you are using the register directive to load the
userconoreol (I mean design time loading), Then page load
of the suer control will run even if the vicible property
= false. This is to initialize the control.

2&3. If you want to dynamically load the user control from
code behind please use the loadcontrol( ) method with
refernce to *.ascx file. No need to use both register &
loadcontrol simultaniously.

4. If you enbael the page caching for your usercontrol (I
mean fragment caching) then it wont take much time to load
the usercontrol next time.

The following links will be usefull.
http://samples.gotdotnet.com/quickstart/aspplus/doc/webpage
lets.aspx
http://www.codeproject.com/aspnet/aspnetusercontrol2.asp
http://www.dotnetjohn.com/articles/articleid52.aspx

regards
Sreejumon[MVP]
DOTNET makes IT happen
 
K

Kurt

John

Have you gotten worked out your solution to loading your controls
dynamically, it sounds like you are having close to the same problrems I am
having.

Kurt
 
J

John

Hi Kurt,

I have finally got it working, although now I'm treading water in another
area (also user control related). If there is anything in particular I could
help with, let me know.

Regards
John.

(e-mail address removed)
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top