Accessing Child Controls in a composite custom control

Discussion in 'ASP .Net Web Controls' started by Buddy Ackerman, Dec 15, 2005.

  1. 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?
     
    Buddy Ackerman, Dec 15, 2005
    #1
    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. sleigh
    Replies:
    1
    Views:
    2,694
    sleigh
    Feb 12, 2004
  2. rodchar
    Replies:
    0
    Views:
    397
    rodchar
    Nov 26, 2007
  3. Moldy

    Accessing Properties of Custom Controls child Controls

    Moldy, Sep 4, 2003, in forum: ASP .Net Building Controls
    Replies:
    8
    Views:
    227
    Moldy
    Sep 5, 2003
  4. Mike J.
    Replies:
    1
    Views:
    177
    John Saunders
    Nov 24, 2003
  5. Paul Yanzick
    Replies:
    4
    Views:
    505
    Steven Cheng[MSFT]
    Jan 9, 2006
Loading...

Share This Page