By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,702 Members | 1,704 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,702 IT Pros & Developers. It's quick & easy.

Custom control within a custom control

P: n/a
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

May 18 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

May 26 '06 #2

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

Charlie
"J R M" <jm*******@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
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

Jun 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.