Ajax TabContainer: Finding Controls

Last post 07-05-2009 11:25 PM by PeteNet. 5 replies.

Sort Posts:

  • Ajax TabContainer: Finding Controls

    07-03-2009, 10:00 PM
    • Member
      454 point Member
    • drpcken
    • Member since 12-15-2005, 5:45 PM
    • Jackson, TN
    • Posts 342

    I have a TabContainer with some TabPanels.  I wrote this loop through it and find specific controls:

    foreach (TabPanel t in ContactTabContainer.Tabs)
            {
                if (t.ID == "DentalPanel")
                {
                    foreach (object ctrl in t.Controls)
                    {
                        if (ctrl is Control)
                        {
                            Control c = (Control)ctrl;
                            foreach (object innerCtrl in c.Controls)
                            {
                                if (innerCtrl is TextBox)
                                {
                                    TextBox txt = (TextBox)innerCtrl;
    
                                    //code
    
    
                                }
                            }
                        }
                    }
                }
            }




    Is there a more efficient way of doing this?  This seems so cumbersome to me.  I could be wrong and it may be exactly right, but I need someone elses opinion.


    Thanks!

  • Re: Ajax TabContainer: Finding Controls

    07-04-2009, 3:18 AM
    • All-Star
      26,488 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,772

    yes, you'd rather use recursion, passing in the control you're looking for. in fact, it is pretty common to have such a helper in the base class so that it can be used anywhere on the pages etc.

    here are a few links: http://www.west-wind.com/Weblog/posts/5127.aspx (follow through with the comments too)

    and here's another one: http://stevesmithblog.com/blog/recursive-findcontrol/

    Regards,
    Peter
  • Re: Ajax TabContainer: Finding Controls

    07-05-2009, 10:09 PM
    • Member
      454 point Member
    • drpcken
    • Member since 12-15-2005, 5:45 PM
    • Jackson, TN
    • Posts 342

    I've used this in the past very well, my problem is I won't know the control's ID, just the type. 

  • Re: Ajax TabContainer: Finding Controls

    07-05-2009, 10:41 PM
    • All-Star
      26,488 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,772

    what would you mean?!!?? are you refering to the links I sent you?

    the functions return a Control type, you can access all properties and methods from that Control object, for example:

    FindControlRecursive return FoundCtl....so you should get FoundCtl.ID and FoundCtl.ClientID etc

    Regards,
    Peter
  • Re: Ajax TabContainer: Finding Controls

    07-05-2009, 10:45 PM
    • Member
      454 point Member
    • drpcken
    • Member since 12-15-2005, 5:45 PM
    • Jackson, TN
    • Posts 342

    Right, but if I'm looking at this right the function requires a parameter with a string datatype, which is the ID of the control to be returned.  In my case I don't know the ID, just the type.

    public static Control FindControlRecursive(Control Root, string Id)




  • Re: Ajax TabContainer: Finding Controls

    07-05-2009, 11:25 PM
    Answer
    • All-Star
      26,488 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,772

    its not as if you can't change the function signature to accept only one parameter and return a List<Control>...that would work in your case..in fact I would pass in the type that I want to find, as the second parameter...and then inside the function I would check for that type and add them to the List to be returned...something like:

          List<Control> controls = new List<Control>();

        if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox")
    && c.ID == null)
    {
    // ...do something...
    controls.Add(c);
    }

    Regards,
    Peter
Page 1 of 1 (6 items)