This is a migrated thread and some comments may be shown as answers.

RadComboBox remove all items and repopulate

1 Answer 900 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Nikos
Top achievements
Rank 1
Nikos asked on 29 Feb 2016, 10:59 PM

I have two related RadComboBoxes as following:

<telerik:RadAjaxManagerProxy ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadDropDownParameterX">
                 <UpdatedControls>
                     <telerik:AjaxUpdatedControl ControlID="RadComboBoxCountries" LoadingPanelID="AjaxLoadingPanel1" />
                     <telerik:AjaxUpdatedControl ControlID="RadComboBoxSites" LoadingPanelID="AjaxLoadingPanel1" />
                     <telerik:AjaxUpdatedControl ControlID="RadComboBoxRts" LoadingPanelID="AjaxLoadingPanel1" />
                     <telerik:AjaxUpdatedControl ControlID="RadComboBoxMachines" LoadingPanelID="AjaxLoadingPanel1" />
                     <telerik:AjaxUpdatedControl ControlID="RadComboBoxProtocols" LoadingPanelID="AjaxLoadingPanel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            <telerik:AjaxSetting AjaxControlID="RadComboBoxCountries">
                 <UpdatedControls>
                     <telerik:AjaxUpdatedControl ControlID="RadCodeBlock1" LoadingPanelID="AjaxLoadingPanel1" />
                     <telerik:AjaxUpdatedControl ControlID="RadCodeBlock2" LoadingPanelID="AjaxLoadingPanel1" />
                     <telerik:AjaxUpdatedControl ControlID="RadComboBoxSites" LoadingPanelID="AjaxLoadingPanel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
            ...
</telerik:RadAjaxManagerProxy>
...
<telerik:RadAjaxLoadingPanel ID="AjaxLoadingPanel1" runat="server" Skin="BlackMetroTouch">
</telerik:RadAjaxLoadingPanel>
...
<div class="right">
    <span>Select X-parameter</span>
    <telerik:RadDropDownList ID="RadDropDownParameterX" runat="server" 
        DropDownHeight="120px" Width="120px" Skin="BlackMetroTouch" DropDownWidth="120px"
            OnSelectedIndexChanged="RadDropDownParameterX_SelectedIndexChanged"
            AutoPostBack="true" >
            <Items>
                <telerik:DropDownListItem Text="Countries" Value="Countries" Selected="true" />
                <telerik:DropDownListItem Text="Sites" Value="Sites" />
                <telerik:DropDownListItem Text="Rts" Value="Rts" />
                <telerik:DropDownListItem Text="Machines" Value="Machines" />
                <telerik:DropDownListItem Text="Protocols" Value="Protocols" />
            </Items>
</telerik:RadDropDownList>
...    
<div class="combo countries">
    <telerik:RadComboBox ID="RadComboBoxCountries" runat="server"
        AutoPostBack="True"
        onselectedindexchanged="RadComboBoxCountries_SelectedIndexChanged"
        CheckBoxes="true" EnableCheckAllItemsCheckBox="true"
        Label="Country" Skin="BlackMetroTouch" >                           
    </telerik:RadComboBox>
    <div class="subv"><telerik:RadCodeBlock ID="RadCodeBlock1" runat="server"> <%= SelectedCountriesText %></telerik:RadCodeBlock></div>
</div>
             
<div class="combo sites">
    <telerik:RadComboBox ID="RadComboBoxSites" runat="server"
        CheckBoxes="true"
        AutoPostBack="true"
        onselectedindexchanged="RadComboBoxSites_SelectedIndexChanged"
        EnableCheckAllItemsCheckBox="true"
        Label="Site" Skin="BlackMetroTouch">
    </telerik:RadComboBox>
    <div class="subv"><telerik:RadCodeBlock ID="RadCodeBlock2" runat="server"> <%= SelectedSitesText %></telerik:RadCodeBlock></div>
</div

What i am trying to achieve is to have the second comboBox (RadComboBoxSites) populated upon selected Values of first comboBox.

Tried to achive that by deleting all items of RadComboBoxSites each time a SelectedIndexChanged event is occured on comboBox RadComboBoxCountries in the way depicted below:

Protected Sub RadComboBoxCountries_SelectedIndexChanged(ByVal o As Object, ByVal e As Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs)
    If (RadDropDownParameterX.SelectedValue.Equals("Sites"))
        Dim collectionPopulatedSites = RadComboBoxSites.Items
        For each s In collectionPopulatedSites
            Dim RadComboBoxSite As RadComboBoxItem = RadComboBoxSites.Items.FindItemByValue(s.Value)
            If RadComboBoxSite IsNot Nothing Then
                RadComboBoxSite.Remove
            End If
        Next
             
        ' get new list of selected countries
        Dim collectionSelectedCountries = RadComboBoxCountries.CheckedItems
        For each c in collectionSelectedCountries
 
            Dim listaCountrySites As List(Of AffideaGeneralWeb.BLL.General.Sites)
            listaCountrySites = GetAllCountrySites(c.Value)
                 
            For each s in listaCountrySites
                     
                Dim RadComboBoxSite As RadComboBoxItem = RadComboBoxSites.Items.FindItemByValue(s.ID)
 
                If RadComboBoxSite Is Nothing Then
                    RadComboBoxSite = New RadComboBoxItem()
                    RadComboBoxSite.Text = s.Title
                    RadComboBoxSite.Value = s.ID
                    RadComboBoxSites.Items.Add(RadComboBoxSite)
                End If
                     
            Next
        Next
 
    End If
 
End Sub

However when i am trying to "RadComboBoxSite.Remove" i am getting an exception:

An exception of type 'System.InvalidOperationException' occurred in mscorlib.dll but was not handled in user code. Additional information: The collection has changed, the enumeration operation may not be performed

Is there any working example of related comboBoxes (handles in the server side)? 

How could i delete all items of second comboBox and re-build it, each time the user alters selection on the first comboBox?

1 Answer, 1 is accepted

Sort by
0
Nencho
Telerik team
answered on 03 Mar 2016, 02:00 PM
Hello Nikos,

The reason for the experienced issue is the fact that in the loop where you iterate trough all the items in the SitesComboBox - you are changing the ItemsCollection of the item. Hence the error occurs, because while you are looping trough the collection its state is changed.

In order to remove the items, I would suggest you to instead of looping trough the items to directly use the Clear method in the Items collection:

RadComboBoxSites.Items.Clear()

Regards,
Nencho
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Ajax
Asked by
Nikos
Top achievements
Rank 1
Answers by
Nencho
Telerik team
Share this question
or