Web form Controls collection

D

dwa

How can a control be added to a table cell within a ascx, so that iteration
over the forms control collection finds the control?

We've got code similar to this (excuse the psuedo-code) in the Page Load of
our the ascx:

// create and initialize a drop down...
DropDownList aDropDown = new DropDownList()
aDropDown.ID = "foo";
aDropDown.Attributes.Add("runat", "server");
etc...

// position the drop down in a table cell....
tableCell = this.FindControl( "tableCellID" );
tableCell.Controls.Add( aDropDown );

Later on, within the scope of the page load, we iterate over the controls on
the form, but we never see our "foo" drop down list.

Any clues?

-- dwa
 
A

Alessandro Zifiglio

hi dwa,
you shouldnt stop at the forms collection. You are adding your control to
your table, so first the pages controls collection, then the form object
controls collection, next your table control and loop through its cells then
the controls collection of the cell to find your dropdownlist.
 
D

dwa

you shouldnt stop at the forms collection. You are adding your control to
your table, so first the pages controls collection, then the form object
controls collection, next your table control and loop through its cells then
the controls collection of the cell to find your dropdownlist.

Right - this would work. But I'm curious to know why the other controls on
my .ascx, which are embedded inside table cells, can be found without this
kind of recursive decent. It's only the one we added manually that
requires this.

- dwa
 
A

Alessandro Zifiglio

dynamically loaded controls dont go in your form objects controls
collection. however you have added your control to another control thats
sitting in your forms objects controls collection. So to be able to find it
you have to get to it through its container that really holds it.
 
D

dwa

Yes - that's what we'll do.

The .ascx that contains the controls I'm talking about itself gets loaded
dynamically. Within the page load of the control, we iterate over all of
it's sub-controls, using the control ids to fabricate keys from which we
perform initialization out of a database. For the most part, the control
and all of it's sub-controls are generated code (from an XML doc) using a
tool that we authored. In most cases, the contents of the sub-controls
can be static - whatever was initialized from the XML document. But in this
case, we needed to make the contents of the lists dynamic - that's where we
encountered the problem.

We'll decend the tree to solve the problem. But it STILL doesn't make
sense - if I add a control to a table cell at design time and I can access
it by looping over the form control collection, without recursive decent,
then why should another control be treated any differently. Seems
asymetrical to me!

Thanks,

-- dwa
 

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
474,093
Messages
2,570,607
Members
47,226
Latest member
uatas12

Latest Threads

Top