How to loop to all tabpanels in a tabcontainer?

zachvaldez used Ask the Experts™
Id like to loop to all my tabpanels and controls within in. Can someone show me how to do it?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Here's a code example of how to loop through the TabPanels, and the Controls.

Here's just a quick overview:

It will take your TabContainer and cast it as a TabContainer, and feed it into a local variable.
Then loops through the container looking for Tab Panels.

It then does the same casting with the TabPanel and then loops through it looking for Controls.
And then just as an example - if the control is a Textbox, it will print out the Text of the text box.

protected void btnLoop_Click(object sender, EventArgs e)
        AjaxControlToolkit.TabContainer container = (AjaxControlToolkit.TabContainer)TabContainer1;
        foreach (object obj in container.Controls)
            if (obj is AjaxControlToolkit.TabPanel)
                AjaxControlToolkit.TabPanel tabPanel = (AjaxControlToolkit.TabPanel)obj;
                foreach (object ctrl in tabPanel.Controls)
                    if (ctrl is Control)
                        Control c = (Control)ctrl;
                        foreach (object innerCtrl in c.Controls)
                            if (innerCtrl is System.Web.UI.WebControls.TextBox)


Protected Sub btnLoop_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim container As AjaxControlToolkit.TabContainer = CType(TabContainer1, AjaxControlToolkit.TabContainer)
            For Each obj As Object In container.Controls
                  If TypeOf obj Is AjaxControlToolkit.TabPanel Then
                        Dim tabPanel As AjaxControlToolkit.TabPanel = CType(obj, AjaxControlToolkit.TabPanel)
                        For Each ctrl As Object In tabPanel.Controls
                              If TypeOf ctrl Is Control Then
                                    Dim c As Control = CType(ctrl, Control)
                                    For Each innerCtrl As Object In c.Controls
                                          If TypeOf innerCtrl Is System.Web.UI.WebControls.TextBox Then
                                                Response.Write((CType(innerCtrl, TextBox)).Text)
                                          End If
                                    Next innerCtrl
                              End If
                        Next ctrl
                  End If
            Next obj
      End Sub

Open in new window


BTW, what does Response.write does? I've never wrote this line before. Thanks for your response.
I have radiobuttonlist,dropdownboxes,labels and textboxes in the panels.
Actually, I dropped a usercontrol in all the panels.

Response.Write just write a string to the page, it's mostly used for Debugging, or just creating a very quick text based page.

For all of your other controls, it's a simple as adding more if statements:

( If TypeOf innerCtrl Is System.Web.UI.WebControls.Label Then )

Just change the System.Web.UI.WebControls.Label to whatever the type of control you're looking for,
then you just have to make sure you recast it inside the if statement [ (CType(innerCtrl, TextBox)) ] to pull whatever you need.


How would I interface if data is coming from database?-Like on page load, i d like to populate the tabcontainer's panels with values.For example in first table
In my table I have the following

Day     time          Languaget    Countof Attendees   ID
Sat     7:30 AM    English                   20                    333
Sun    9:30 AM    English                    30                    400
Sun  ......

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial