Composite controls

Discussion in 'ASP .Net' started by martin, Nov 1, 2003.

  1. martin

    martin Guest

    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.
     
    martin, Nov 1, 2003
    #1
    1. Advertising

  2. "martin" <> wrote in message
    news:...
    > 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));

    --
    John
     
    John Saunders, Nov 2, 2003
    #2
    1. Advertising

  3. martin

    martin Guest

    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.



    "John Saunders" <john.saunders at surfcontrol.com> wrote in message
    news:...
    > "martin" <> wrote in message
    > news:...
    > > 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));
    >
    > --
    > John
    >
    >
     
    martin, Nov 2, 2003
    #3
  4. "martin" <> wrote in message
    news:...
    > 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.
    --
    John
     
    John Saunders, Nov 4, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sleigh
    Replies:
    1
    Views:
    2,694
    sleigh
    Feb 12, 2004
  2. dx
    Replies:
    10
    Views:
    950
    John Saunders
    Apr 11, 2004
  3. Ziyad Makki
    Replies:
    0
    Views:
    308
    Ziyad Makki
    Jul 20, 2004
  4. Chad
    Replies:
    0
    Views:
    225
  5. Mike

    Composite vs non composite Controls

    Mike, Mar 10, 2005, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    266
    Sundararajan
    Mar 11, 2005
Loading...

Share This Page