Custom control within a custom control

Custom control within a custom control

Post by J R » Fri, 19 May 2006 12:23:16


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
 
 
 

Custom control within a custom control

Post by J R » Sun, 28 May 2006 03:58:27

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

 
 
 

Custom control within a custom control

Post by Charli » Sat, 03 Jun 2006 03:04:54

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

Charlie