Composite controls

M

martin

Hi,

I am attemping to write a composite control but have so far not achieved the
results that I am looking for and would appreciate some advice.
By a composite control, i mean, a Web User control that has another web user
control passed to it as one of the arguments in the containing aspx page.

For example say I have a web page MyPage.aspx

Inside MyPage.aspx I have a user control "Control1.ascx"

I have another user control "Control2.ascx" that I would like to include in
Control1, I do not want to hard code "Control2.ascx" inside "Control1.ascx"
because in another page I might want to pass "Control3.ascx" as an argument
to "Control1.ascx"

My prolem is how to make a user control an arument to another control.
The best method I have come up with so far is to pass the path of user
"control2.ascx" to "Control1.ascx" as an argument of type string and then
use "Loadcontrol" to dynamically load the control, but this way seems messy
to me (as well as a little difficult to master..) so I was hoping for some
advice off anybody who had achieved a similar thing.

cheers

martin.
 
J

John Saunders

martin said:
Hi,

I am attemping to write a composite control but have so far not achieved the
results that I am looking for and would appreciate some advice.
By a composite control, i mean, a Web User control that has another web user
control passed to it as one of the arguments in the containing aspx page.

For example say I have a web page MyPage.aspx

Inside MyPage.aspx I have a user control "Control1.ascx"

I have another user control "Control2.ascx" that I would like to include in
Control1, I do not want to hard code "Control2.ascx" inside "Control1.ascx"
because in another page I might want to pass "Control3.ascx" as an argument
to "Control1.ascx"

My prolem is how to make a user control an arument to another control.
The best method I have come up with so far is to pass the path of user
"control2.ascx" to "Control1.ascx" as an argument of type string and then
use "Loadcontrol" to dynamically load the control, but this way seems messy
to me (as well as a little difficult to master..) so I was hoping for some
advice off anybody who had achieved a similar thing.

I don't really see any other way for you to do it. There are two ways to
instantiate an User Control. One is to put it on a page or other user
control, in which case it will appear within that page or user control,
which you don't want. The other way is to call LoadControl...

The only trick to it is that you have to remember to load Control2 on every
request, not just if (!Page.IsPostBack). Also, you'll have to have a
PlaceHolder inside of Control1 and you'll have to Add the loaded Control2 to
that PlaceHolder's Controls collection:

myPlaceHolder.Controls.Add(LoadControl(controlToLoad));
 
M

martin

John,

Thanks for the response.
However I am having trouble loading the usercontrol2 inside usercontrol1 if
I miss out the

<@reference control='UserControl2'> inside user control1.

I am kinda hoping that to dynamically include a control you don not have to
hard code a reference to it in the page.
else this sort of defeats the purpose of dynamically including it with
LoadControl(..).

I don't know if you can offer me any further advice on this issue or point
me to an article that may feature a similar thing.

cheers for the advice adding to the Placeholders controls collection, handy
there is no <form1 ..> in a user control.

cheers

martin.
 
J

John Saunders

martin said:
John,

Thanks for the response.
However I am having trouble loading the usercontrol2 inside usercontrol1 if
I miss out the

<@reference control='UserControl2'> inside user control1.

Martin, you're saying you're having trouble, but you don't say what the
trouble is, nor do you show us the code which is having the trouble.
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top