Accessing Child Controls in a composite custom control

B

Buddy Ackerman

I've created a custom control with several child controls (like checkboxes
and textboxes). However, in order to have a nice layout I added all of the
text boxes and checkboxes to a table control like this:

Table t = new Table();
TableRow tr = new TableRow();
TableCell td1 = new TableCell();
TableCell td2 = new TableCell();
td1.VerticalAlign = VerticalAlign.Top;
Label lblName = new Label();
lblName.Text = "Product Name: ";
td1.Controls.Add(lblName);

productname.ID = "productname";
productname.Columns = 70;
td2.Controls.Add(productname);
tr.Cells.Add(td1);
tr.Cells.Add(td2);
t.Rows.Add(tr);

So, my custom control has properties that pass through to the child textbox
and checkbox controls. I had this working before i added the table for
layout but on since the addition of the child controls to the custom
controls ControlsCollection is not as orderly I can't figure out how to
reference the child control anymore I've tried this:

public string ProductName
{
get
{
EnsureChildControls();
return
((TextBox)this.Controls[this.Controls.IndexOf(productname)]).Text;
}
set
{
EnsureChildControls();
((TextBox)this.Controls[this.Controls.IndexOf(productname)]).Text =
value;
}
}

The IndexOf method doesn't seem to be able to find the control (all of the
chilc controls, like productname, are declared at the class level).

So how do I reference child controls when there is no orderly way of adding
them to the Controls collection?
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top