Avatar of David Modugno
David Modugno
 asked on

how to loop threw all combo boxes on a page

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

Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
Mlanda T

8/22/2022 - Mon
chaau

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

it_saige

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-
David Modugno

ASKER
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

Your help has saved me hundreds of hours of internet surfing.
fblack61
Mlanda T

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?
David Modugno

ASKER
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..
Mlanda T

Perhaps you might have to show us some more of the code
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
David Modugno

ASKER
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

ASKER CERTIFIED SOLUTION
Mlanda T

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
David Modugno

ASKER
Thank you for the help and the lesson.. that works perfect
Mlanda T

Glad to help
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes