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

[Solved] Add Item into RadCombobox from Radtextbox

3 Answers 326 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Antony
Top achievements
Rank 1
Antony asked on 27 Feb 2013, 07:54 AM
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.

3 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 27 Feb 2013, 09:05 AM
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.
0
Antony
Top achievements
Rank 1
answered on 27 Feb 2013, 03:24 PM
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()?
0
Accepted
Princy
Top achievements
Rank 2
answered on 28 Feb 2013, 06:26 AM
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.
Tags
ComboBox
Asked by
Antony
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Antony
Top achievements
Rank 1
Share this question
or