ajax 如何在 RadGrid 的 FormTemplate 中设置 Telerik RadComboBox 的 SelectedValue 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2741163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the SelectedValue attribute of Telerik RadComboBox inside the FormTemplate of a RadGrid
提问by Kyle Noland
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.
我有一个我认为应该是一个直截了当的问题。我有一个启用了 FormTemplate 编辑和 AJAX 的 RadGrid。FormTemplate 中的字段之一是填充美国州选择的 RadComboBox。我可以将 RadComboBox 绑定到数据源以填充所有项目,但我无法设置 SelectedValue 属性。
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.
当单击 RadGrid 上一行的“编辑”按钮时,会加载此 RadComboBox。使用自定义 FormTemplate 并通过 AJAX 加载正在编辑的行的内容。
回答by Serapth
If you are DataBinding, its literally as easy as adding
如果您是 DataBinding,它实际上就像添加一样简单
SelectedValue='<%# Bind("FieldName")%>'
Inside the FormTemplate declaration of the RadComboBox.
在 RadComboBox 的 FormTemplate 声明中。
If you however want to programmatically determine what value to select, then you need to implement ItemDataBound in the RadGrid, like the following example:
但是,如果您想以编程方式确定要选择的值,则需要在 RadGrid 中实现 ItemDataBound,如下例所示:
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;
}
}
回答by sathish
clear all items of radcombobox initially and then add a new item manually
最初清除 radcombobox 的所有项目,然后手动添加新项目
this is what i do to set new item when i use web service
这是我在使用 Web 服务时设置新项目的方法
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

