3

I have what I think should be a straightforward question. I have a RadGrid with FormTemplate editing and AJAX enabled. One of the fields in the FormTemplate is a RadComboBox filled with U.S. State selections. I can bind the RadComboBox to the data source to populate all the items, but I'm not able to set the SelectedValue attribute.

This RadComboBox is loaded when the Edit button is clicked for a row on the RadGrid. A custom FormTemplate is used and the contents of the row being edited are loaded via AJAX.

Kyle Noland
  • 4,788
  • 6
  • 30
  • 33
  • Are you trying to set the value client side in Javascript, or server side in code? Also, is the SelectedValue supposed to be databound or is it programmatically selected? – Serapth Apr 29 '10 at 22:29
  • I would like to set SelectedValue server side, declaratively. I already know what the value is. SelectedValue does not appear to be a declarative option. Also, I'm not sure how to set SelectedValue in code behind because this is an AJAX enabled RadGrid. – Kyle Noland Apr 29 '10 at 22:35

2 Answers2

5

If you are DataBinding, its literally as easy as adding

SelectedValue='<%# Bind("FieldName")%>'

Inside the FormTemplate declaration of the RadComboBox.

If you however want to programmatically determine what value to select, then you need to implement ItemDataBound in the RadGrid, like the following example:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
       if (e.Item is GridEditFormItem && e.Item.IsInEditMode) 
        { 
            GridEditFormItem editFormItem = (GridEditFormItem)e.Item; 
            RadComboBox combo = (RadComboBox)editFormItem.FindControl("yourControlName"); 
            combo.SelectedValue= Somevalue;
        } 
    } 
Serapth
  • 7,122
  • 4
  • 31
  • 39
  • It should be, but Visual Studio acts like the SelectedValue attribute doesn't exist for this control. Including it anyway throws the following exception: Message: Sys.WebForms.PageRequestManagerServerErrorException: Selection out of range Parameter name: value – Kyle Noland Apr 29 '10 at 23:20
  • Here is my control: – Kyle Noland Apr 29 '10 at 23:21
  • You may have a problem with your Telerik install. If you pull your RadComboBox out of the Grid so its on its own, does everything work? If you can, edit your first post to include the entire markup of the RadGrid, especially the FormTemplate section. – Serapth Apr 29 '10 at 23:28
  • 2
    One possible cause of that problem is if the SelectedValue doesn't match up with the available Values. – Serapth Apr 29 '10 at 23:30
  • Pulling the RadComboBox out of the Grid still does not let me set a SelectedValue. I am using Visual Studio 2010 and the Q1 2010 RadControl for ASP.NET AJAX. – Kyle Noland Apr 29 '10 at 23:43
  • At this point, without more code to look at, I don't think I can help you. I am using Rad Controls with VS2k10 aswell, but I havent upgraded from Q3 2009 yet, but I doubt that is the issue. By the way, if the ComboBox is inside a DataBound grid and shares the same Datasource, don't specify DataSourceID. – Serapth Apr 30 '10 at 00:21
1

clear all items of radcombobox initially and then add a new item manually

this is what i do to set new item when i use web service

     ddl.ClearSelection()
            ddl.Items.Clear()

'below i'm getting the actual value and the text to display
            Using reader As IDataReader = GetClientByClientID(CInt(value))
                If reader.Read Then

'adding the item will show in the dropdown
                    Dim item As New RadComboBoxItem(reader("DisplayName").ToString, reader("ID").ToString)
                    item.Selected = True
                    ddl.Items.Add(item)

'this line will make the combobox text to be displayed correctly
                    ddl.Text = reader("DisplayName").ToString

                    ddl.DataBind()
                Else
                    ddl.Text = ""

                    ddl.ErrorMessage = "Selected Client Not Found !"
                End If

                reader.Close()
            End Using
sathish
  • 11
  • 1