how to loop threw all combo boxes on a page

David Modugno
David Modugno used Ask the Experts™
on
I have 13 combo boxes that the selected item is decided based on the result of a sql query.
I want to check all of the combo boxes for a value of "Not Done" and then set the background color to a different color.
I have named the combo boxes cmb1, cmb2, cmb2 and so on... I thought I could do something like this.. but I was wrong

For i As Integer = 1 To 13
            If cmb(i).....  then
                 do stuff

            End If

I think I might be way off here on how to pull this off.. any help would be great


        Next

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2013

Commented:
You need to use the Controls collection to loop throw the controls on the page:
For Each cntrl As Control In Me.Controls
    If TypeOf cntrl Is ComboBox Then
        Dim cbobx As ComboBox = CType(cntrl, ComboBox)
        If cbobx.Text = "not selected" Then
            'Do Something
        End If
    End If
Next

Open in new window

Commented:
I would recommend one change to chaau's proposed solution:
For Each cntrl As Control In Me.Controls
    If TypeOf cntrl Is ComboBox Then
        Dim cbobx As ComboBox = CType(cntrl, ComboBox)
        If String.Equals(cbobx.Text, "not done", StringComparison.OrdinalIgnoreCase) Then
            'Do Something
        End If
    End If
Next

Open in new window

-saige-

Author

Commented:
I gave it a go, but had no luck... I should have said that I am using telerik controls.. not sure if it makes a difference.. here is what I have ... thanks for the help

 For Each cntrl As Control In Me.Controls
            If TypeOf cntrl Is RadComboBox Then
                Dim cbobx As RadComboBox = CType(cntrl, RadComboBox)
                If cbobx.Text = "Not Done" Then
                    cbobx.BackColor = Color.Orange
                End If
            End If
        Next

Open in new window

CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

It should not matter that you are using Telerik. Your code looks correct (I also use Telerik a lot). The concept is the same. You need to tell us more about what exactly has not worked here.

If you put a breakpoint on line 3 on your last snippet, do you reach that breakpoint? In other words, are you finding any of the RadComboBoxes?  
In which method are you doing this... Hopefully not Page_Init? Coz maybe your changes get overwritten when ViewState is restore AFTER the Page_Init call
You are looping through Me.Controls... Is that the parent container closest to the radcomboboxes? You might be looping through the wrong controls collection...
What sort of controls are you finding in Me.Controls? Should you recursively loop through their own controls collections as well?

Author

Commented:
Break point... It does not reach it

right now I have the code behind a button (for testing)..
after the page loads.. all combo boxes are filled and the correct value is selected I click the button expecting it to change the background color

you will have to forgive me on the next two ?'s.. I'm not a very experienced programmer and am not exactly sure what you are asking

its a single asp page with all the controls on it and vb.net code behind..
Perhaps you might have to show us some more of the code

Author

Commented:
I think I understand what you are saying, but don't know what to do about it.  the combo boxes are inside of a container

Here is some of the code

<telerik:RadSplitter ID="RadSplitter3" runat="server" SplitBarsSize="" LiveResize="True" Width="1200px" Height="600px">
                       <telerik:RadPane ID="RadPane5" Runat="server" Height="600px" Width="700px">
                            <br /><br />
                            <table class="nav-justified">
                                <tr>
                                    <td class="auto-style111">&nbsp;</td>
                                    <td class="auto-style112">STATUS</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                </tr>
                                <tr>
                                    <td class="auto-style111">Survey Complete</td>
                                    <td class="auto-style112">
                                        <telerik:RadComboBox ID="cmb1" Runat="server">
                                            <Items>
                                                <telerik:RadComboBoxItem runat="server" Text="Done" Value="Done" />
                                                <telerik:RadComboBoxItem runat="server" Text="Not Done" Value="Not Done" />
                                                <telerik:RadComboBoxItem runat="server" Text="N/A" Value="N/A" />
                                            </Items>
                                        </telerik:RadComboBox>
                                    </td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                </tr>

Open in new window

Try this. Note that I have changed the For loop. It now loops through RadPane5.Controls  
 For Each cntrl As Control In RadPane5.Controls
            If TypeOf cntrl Is RadComboBox Then
                Dim cbobx As RadComboBox = CType(cntrl, RadComboBox)
                If cbobx.Text = "Not Done" Then
                    cbobx.BackColor = Color.Orange
                End If
            End If
        Next

Open in new window

Now, to understand the server side code? You say breakpoint is not being hit... At what point are you long through the controls? Is it on a button click? Is that button click event being hit at all?

Author

Commented:
Thank you for the help and the lesson.. that works perfect
Glad to help

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