Transfer ComboBox.CheckedItems to ListBox

6 posts, 2 answers
  1. GARY078
    GARY078 avatar
    11 posts
    Member since:
    Jun 2012

    Posted 26 Sep 2012 Link to this post

    Hi,

    I'm attempting to show the end user some feedback on the items they've checked in a combobox and I want to send the checkedItems to a ListBox underneath showing what the user has checked. I'm having no luck with what I am trying.

    Something like this:

    ListBox1.Items.Add(ComboBox.CheckedItems.ToString());
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 27 Sep 2012 Link to this post

    Hi Gary,

    I suppose you want to copy the checked RadComboBoxItems to the RadListBox. Following is the sample code that I tried.

    ASPX:
    <telerik:RadComboBox ID="RadComboBox1" runat="server" CheckBoxes="true">
      <Items>
        <telerik:RadComboBoxItem Text="RadComboBoxItem1" />
        <telerik:RadComboBoxItem Text="RadComboBoxItem2" />
        <telerik:RadComboBoxItem Text="RadComboBoxItem3" />
        <telerik:RadComboBoxItem Text="RadComboBoxItem4" />
        <telerik:RadComboBoxItem Text="RadComboBoxItem5" />
        <telerik:RadComboBoxItem Text="RadComboBoxItem6" />
      </Items>
    </telerik:RadComboBox>
    <asp:Button ID="btn" runat="server" text="transfer" OnClick="btn_Click" />
    <telerik:RadListBox ID="RadListBox2" runat="server">
    </telerik:RadListBox>

    C#:
    protected void btn_Click(object sender, EventArgs e)
        {
            foreach (RadComboBoxItem item in RadComboBox1.CheckedItems)
            {
                RadListBoxItem itm = new RadListBoxItem();
                itm.Text = item.Text;
                RadListBox2.Items.Add(itm);
                RadListBox2.DataBind();
            }
             
        }

    Hope this helps.

    Regards,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. GARY078
    GARY078 avatar
    11 posts
    Member since:
    Jun 2012

    Posted 28 Sep 2012 Link to this post

    Thanks Princy!

    Would this method still work with a databound ComboBox?
  5. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 28 Sep 2012 Link to this post

    Hi,

    You can use the same approach for a data bound combobox as well.
    aspx:
    <telerik:RadComboBox ID="RadComboBox1" DataSourceID="SqlDataSource2" DataTextField="EmployeeID" DataValueField="EmployeeID" runat="server" CheckBoxes="true">
     </telerik:RadComboBox>
    <asp:Button ID="btn" runat="server" text="transfer" OnClick="btn_Click" />
    <telerik:RadListBox ID="RadListBox2" runat="server">
    </telerik:RadListBox>
    C#:
    protected void btn_Click(object sender, EventArgs e)
        {
            foreach (RadComboBoxItem item in RadComboBox1.CheckedItems)
            {
                RadListBoxItem itm = new RadListBoxItem();
                itm.Text = item.Text;
                RadListBox2.Items.Add(itm);
                RadListBox2.DataBind();
            }
              
        }

    Regards,
    Princy.
  6. GARY078
    GARY078 avatar
    11 posts
    Member since:
    Jun 2012

    Posted 16 Oct 2012 Link to this post

    Is there a client-side method of achieving the same result?

    I can get the items to transfer from combo to listbox, but what do i set the "item.set_text" property as?

    At the moment the itemText is just being passed as "Object"...
    function addItem(sender, eventArgs) {
                    var itemText = $find("<%=BusinessRolesDDL.ClientID %>").get_checkedItems();
                    var listBox = $find("<%=BusinessRolesAdd.ClientID %>");
     
                    if (sender.get_checkedItems() <= 0) {
                        alert("Please choose functional roles to add.");
                        return false;
                    }
     
                    listBox.trackChanges();
                    //Instantiate a new client item
                    var item = new Telerik.Web.UI.RadListBoxItem();
     
                    item.set_text(itemText);
                    listBox.get_items().add(item);
                    item.scrollIntoView();
                    listBox.commitChanges();
                    return false;
                }

  7. Answer
    Nencho
    Admin
    Nencho avatar
    1458 posts

    Posted 17 Oct 2012 Link to this post

    Hi Gary,

    In order to implement transfer client-side, you should loop trough all the checked items from RadComboBox and create a RadListBoxItem with the corresponding Text. I have prepared a sample project for you, so you could implement the suggested approach. Find the attached sample.


    Greetings,
    Nencho
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017