Add Item into RadCombobox from Radtextbox

4 posts, 2 answers
  1. Antony
    Antony avatar
    32 posts
    Member since:
    Feb 2013

    Posted 27 Feb 2013 Link to this post

    Hi All,

    I was struggling with adding items from client side. I got a RadTextbox and RadCombobox. On a radbutton client click, i want to get the textbox value and add it into the Radcombobox. This I was trying with JavaScript but didnt come up with the right code. Please help.

    Antony.
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 27 Feb 2013 Link to this post

    Hello Antony,

    Try the following JavaScript to fetch the RadTextBox value and add it to the RadComboBox on client clicking the RadButton.

    <script type="text/javascript">
        function OnClientClicked(sender, args) {
            var textBox = $find('<%=RadTextBox1.ClientID %>');
            // fetching the RadTextBox value
            var textboxValue = textBox.get_element().value;
            var combo = $find("<%= RadComboBox1.ClientID %>");
            // Creating a new Item.
            var comboItem = new Telerik.Web.UI.RadComboBoxItem();
            // Setting the fetched value as the Item text.
            comboItem.set_text(textboxValue);
            combo.trackChanges();
           // adding item to radcombobox
            combo.get_items().add(comboItem);
            combo.commitChanges();
        }
    </script>

    Thanks,
    Princy.
  3. Antony
    Antony avatar
    32 posts
    Member since:
    Feb 2013

    Posted 27 Feb 2013 Link to this post

    Hi Princy,

    Thanks for your code with a neat explanation.
    The JS code to insert item into the combobox you are writing in between trackChanges and commitChanges. Can you please let me know the exact purpose of this especially the trackChanges()?
  4. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 28 Feb 2013 Link to this post

    Hello Antony,

    When you are making some changes to the RadComboBox from client side it should be available on the server after postback and those codes should be written in between trackChanges and commitChanges methods. If you simple make some changes and try to commit it without calling trackChanges(), those changes wont be reflected in the RadComboBox.

    The purpose of trackChanges() is to start tracking changes made to RadComboBox that will be preserved over post-backs and commitChanges() is to write the changes to RadComboBox that were made since a previous call to trackChanges, so that they are preserved over post-backs.

    Thanks,
    Princy.
Back to Top