如何在RadGrid的FormTemplate中设置Telerik RadComboBox的SelectedValue属性

时间:2010-04-29 22:22:27

标签: ajax telerik radgrid selectedvalue radcombobox

我认为应该是一个直截了当的问题。我有一个带有FormTemplate编辑和启用AJAX的RadGrid。 FormTemplate中的一个字段是一个充满美国州选择的RadComboBox。我可以将RadComboBox绑定到数据源以填充所有项目,但我无法设置SelectedValue属性。

单击RadGrid上的行的“编辑”按钮时,将加载此RadComboBox。使用自定义FormTemplate,并通过AJAX加载正在编辑的行的内容。

2 个答案:

答案 0 :(得分:5)

如果你是DataBinding,它就像添加

一样简单
SelectedValue='<%# Bind("FieldName")%>'

在RadComboBox的FormTemplate声明中。

如果您想以编程方式确定要选择的值,那么您需要在RadGrid中实现ItemDataBound,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;
        } 
    } 

答案 1 :(得分:1)

最初清除所有radcombobox项目,然后手动添加新项目

这是我在使用网络服务时设置新项目的方法

     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