Custom control within a custom control

Discussion in 'ASP .Net' started by J R M, May 18, 2006.

  1. J R M

    J R M Guest

    I've developed a custom control (it's basically a drop-down list and
    then a couple of textboxes to include meta-data for the selection) that
    I'm embedding into another custom control (the idea is to allow a
    dynamic number of control1 to appear in control2). Everything renders
    correctly, however I could never retrieve data on postback from my
    embedded controls. Upon closer inspection, all of the embedded
    controls output without any "name" attributes on them, so the postback
    can't find the data. I've got all of my controls implementing
    INamingContainer which I thought would fix things, but it doesn't.
    Furthermore, if I specify ID's for the embedded controls, they DO get a
    "name" attribute, but it doesn't conform to the typical naming
    heirarchy that aspnet uses to label controls, so I'm still not seeing
    my data on postback. I guess I could try writing some code to search
    for the goofy names to try to retrieve my data, but that feels like a
    clumsy, easily breakable hack.

    So my primary question is: Is it possible to embed custom controls
    within custom controls?

    Lead up to question 2: As I was playing with this, I wondered if
    perhaps my array of embedded controls was to blame, so I tried creating
    just a single instance of my embedded control and the same thing (no
    'name' attr) happened. Just for kicks, I tried generating an array of
    stock TextBox controls and by golly, the same thing happened--the
    textboxes rendered, but the 'name' attribute was empty. (BTW, both the
    custom control and the textbox array were declared as a class instance
    variables and initialized in "OnInit"),

    So question #2 is: Is it possible to have a dynamically-sized
    collection of controls within a custom control? Or am I going to have
    to "fix" this by creating individual instances of everything within a
    single custom control?

    Let me know if I can provide any other info. Thanks for any advice.

    Jay
     
    J R M, May 18, 2006
    #1
    1. Advertising

  2. J R M

    J R M Guest

    Figured this out. Sharing in case anyone else finds this useful.

    I basically had to hotwire the controls to make this work via the
    following steps:

    1. Had to explicitly set all ID's for all embedded controls.
    Implementing INamingContainer wasn't doing anything. Not sure why.

    2. Had to implement IPostBackDataHandler and define the LoadPostData
    method in both the parent and child controls in order to retrieve the
    data for my custom child controls.

    That's basically it, but this gets tricky because of life-cycle stuff.
    The LoadPostData gets fired before the parent page's OnInit, so there
    was a little dancing around I'd have to do to make sure all of my
    objects were defined properly before I could really handle the postback
    data. But it all seems to be working now.

    Jay
     
    J R M, May 26, 2006
    #2
    1. Advertising

  3. J R M

    Charlie@CBFC Guest

    You need to implement IPostBackDataHandler if you want to capture postbacks
    in a Custom Server control.

    Charlie
    "J R M" <> wrote in message
    news:...
    > I've developed a custom control (it's basically a drop-down list and
    > then a couple of textboxes to include meta-data for the selection) that
    > I'm embedding into another custom control (the idea is to allow a
    > dynamic number of control1 to appear in control2). Everything renders
    > correctly, however I could never retrieve data on postback from my
    > embedded controls. Upon closer inspection, all of the embedded
    > controls output without any "name" attributes on them, so the postback
    > can't find the data. I've got all of my controls implementing
    > INamingContainer which I thought would fix things, but it doesn't.
    > Furthermore, if I specify ID's for the embedded controls, they DO get a
    > "name" attribute, but it doesn't conform to the typical naming
    > heirarchy that aspnet uses to label controls, so I'm still not seeing
    > my data on postback. I guess I could try writing some code to search
    > for the goofy names to try to retrieve my data, but that feels like a
    > clumsy, easily breakable hack.
    >
    > So my primary question is: Is it possible to embed custom controls
    > within custom controls?
    >
    > Lead up to question 2: As I was playing with this, I wondered if
    > perhaps my array of embedded controls was to blame, so I tried creating
    > just a single instance of my embedded control and the same thing (no
    > 'name' attr) happened. Just for kicks, I tried generating an array of
    > stock TextBox controls and by golly, the same thing happened--the
    > textboxes rendered, but the 'name' attribute was empty. (BTW, both the
    > custom control and the textbox array were declared as a class instance
    > variables and initialized in "OnInit"),
    >
    > So question #2 is: Is it possible to have a dynamically-sized
    > collection of controls within a custom control? Or am I going to have
    > to "fix" this by creating individual instances of everything within a
    > single custom control?
    >
    > Let me know if I can provide any other info. Thanks for any advice.
    >
    > Jay
    >
     
    Charlie@CBFC, Jun 1, 2006
    #3
    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. Jonah Olsson
    Replies:
    1
    Views:
    585
    Scott Allen
    Apr 5, 2005
  2. KatB
    Replies:
    0
    Views:
    452
  3. =?Utf-8?B?SmF5YnVmZmV0?=
    Replies:
    9
    Views:
    852
    Phillip Williams
    Feb 21, 2006
  4. Matt Sokol
    Replies:
    2
    Views:
    397
    Theo Tillotson
    Aug 7, 2003
  5. Jonah Olsson
    Replies:
    2
    Views:
    208
    Jonah Olsson
    Apr 5, 2005
Loading...

Share This Page