RadComboBox remove all items and repopulate

2 posts, 0 answers
  1. Nikos
    Nikos avatar
    8 posts
    Member since:
    Sep 2015

    Posted 29 Feb 2016 Link to this post

    I have two related RadComboBoxes as following:

    <telerik:RadAjaxManagerProxy ID="RadAjaxManager1" runat="server">
                <telerik:AjaxSetting AjaxControlID="RadDropDownParameterX">
                         <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" />
                <telerik:AjaxSetting AjaxControlID="RadComboBoxCountries">
                         <telerik:AjaxUpdatedControl ControlID="RadCodeBlock1" LoadingPanelID="AjaxLoadingPanel1" />
                         <telerik:AjaxUpdatedControl ControlID="RadCodeBlock2" LoadingPanelID="AjaxLoadingPanel1" />
                         <telerik:AjaxUpdatedControl ControlID="RadComboBoxSites" LoadingPanelID="AjaxLoadingPanel1" />
    <telerik:RadAjaxLoadingPanel ID="AjaxLoadingPanel1" runat="server" Skin="BlackMetroTouch">
    <div class="right">
        <span>Select X-parameter</span>
        <telerik:RadDropDownList ID="RadDropDownParameterX" runat="server" 
            DropDownHeight="120px" Width="120px" Skin="BlackMetroTouch" DropDownWidth="120px"
                AutoPostBack="true" >
                    <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" />
    <div class="combo countries">
        <telerik:RadComboBox ID="RadComboBoxCountries" runat="server"
            CheckBoxes="true" EnableCheckAllItemsCheckBox="true"
            Label="Country" Skin="BlackMetroTouch" >                           
        <div class="subv"><telerik:RadCodeBlock ID="RadCodeBlock1" runat="server"> <%= SelectedCountriesText %></telerik:RadCodeBlock></div>
    <div class="combo sites">
        <telerik:RadComboBox ID="RadComboBoxSites" runat="server"
            Label="Site" Skin="BlackMetroTouch">
        <div class="subv"><telerik:RadCodeBlock ID="RadCodeBlock2" runat="server"> <%= SelectedSitesText %></telerik:RadCodeBlock></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
                End If
            ' 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
                    End If
        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?

  2. Nencho
    Nencho avatar
    1666 posts

    Posted 03 Mar 2016 Link to this post

    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:


    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
Back to Top