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

Setting text and value via Javascript

3 Answers 605 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Jeremy Yoder
Top achievements
Rank 1
Jeremy Yoder asked on 20 Nov 2012, 07:49 PM

On webpage load, I populate a RadComboBox with text and keys. I have a default value I assign then on server-side too.

If they click a button to clear the form, I blank out all controls, but populate defaults. I need to do it client-side, and I'm having trouble with RadComboBox. I thought I'd populate a hidden field with the RadComboBox's default text on server-side during the load, then assign that value client-side when needed. I've tried both set_text (and set_value with the hidden field holding the default value instead) but when it evaluates later, it appears either the text is right but the value isn't, or vice versa.

Do I have to track in 2 hidden fields (both  default text and default value) and assign both client-side? That seems goofy, but if so, in what order should it be done? Or can I do this without tracking both the text and value default?

Example javascript code would be very welcome, assuming the hidden fields are populated... unless I should do it a completely different way.

3 Answers, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 2
answered on 20 Nov 2012, 09:55 PM
Hello Jeremy,

If you're changing anything on the RadComboBox from the client-side you need to call the trackChanges and commitChanges methods, in order for the state of the RadComboBox to persist on the server-side.

You code should look like this:

Javascript:
var combo = $find("<%=RadComboBox1.ClientID%>");
 
combo.trackChanges();
combo.set_value("Test");
combo.commitChanges();

I hope that helps.
0
Jeremy Yoder
Top achievements
Rank 1
answered on 26 Nov 2012, 06:59 PM

This javascript changes the value of the combobox, but the text value displayed does NOT change. I need both to change, or the display conflicts with the actual value.
0
Nencho
Telerik team
answered on 29 Nov 2012, 04:45 PM
Hi Jeremy,

I can suggest you to store both the text and the value of the default item in the HiddenField. You could use a comma as separator and split them in client-side, in order to access the text and the value. I have prepared a sample project for you demonstrate you the suggested approach. Please find the sample attached.

Regards,
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.
Tags
ComboBox
Asked by
Jeremy Yoder
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 2
Jeremy Yoder
Top achievements
Rank 1
Nencho
Telerik team
Share this question
or