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

Preselect an combobox item after callback

3 Answers 137 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
ben
Top achievements
Rank 1
ben asked on 29 Jul 2008, 03:09 PM
Hello all,

I have code on the server-side that populates a combobox during the ItemsRequested event.  This code also knows what value should be preselected after the combobox has been populated.  I tried selecting the item programmatically but it doesn't seem to have any effect on the client.

I know I can also handle the OnClientItemsRequested event and select a item on the client-side after the callback is done.  However, the info i need (what item to select) is on the server-side and I don't know how to pass it back to the client.

Any ideas how this can be done?

Thanks.

3 Answers, 1 is accepted

Sort by
0
Accepted
Veselin Vasilev
Telerik team
answered on 29 Jul 2008, 03:44 PM
Hello ben,

Welcome to Telerik Community!

You cannot set the selected item in the ItemsRequested event, but you can set a custom attribute for the selected item there and in the OnClientItemsRequested handler to check for that attribute and select the item:

    protected void RadComboBox1_ItemsRequested(object o, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e) 
    { 
        RadComboBoxItem item = new RadComboBoxItem("item 1"); 
        RadComboBox1.Items.Add(item); 
 
        item = new RadComboBoxItem("selected item"); 
        item.Attributes["isSelected"] = "true"; 
        RadComboBox1.Items.Add(item); 
    } 

function OnClientItemsRequestedHandler(sender, eventArgs) 
    var items = sender.get_items(); 
     
    for (var i = 0; i < items.get_count(); i++) 
    { 
        if (items.getItem(i).get_attributes().getAttribute("isSelected") == "true"
        { 
            items.getItem(i).select(); 
            break
        } 
    } 

I hope this helps.

Regards,
Veskoni
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
ben
Top achievements
Rank 1
answered on 30 Jul 2008, 07:52 AM
Thanks a lot.  That's exactly what I was looking for.  But now the next problem I'm having is this.  After the callback has been made and combobox repopulated, the user will click on a button to post the form.  How do I get the selected value?  It looks like the selected item was the old one before the callback was made.
0
Veselin Vasilev
Telerik team
answered on 30 Jul 2008, 02:19 PM
Hello ben,

The SelectedValue should do the work.

I have attached my test page. Let me know what is different in your case.


Greetings,
Veskoni
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
ComboBox
Asked by
ben
Top achievements
Rank 1
Answers by
Veselin Vasilev
Telerik team
ben
Top achievements
Rank 1
Share this question
or