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

发布于 2024-08-30 10:23:10 字数 257 浏览 5 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假扮的天使 2024-09-06 10:23:10

如果您使用数据绑定,那么它实际上就像

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

在 RadComboBox 的 FormTemplate 声明内添加一样简单。

但是,如果您想以编程方式确定选择什么值,则需要在 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;
        } 
    } 

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;
        } 
    } 
纵性 2024-09-06 10:23:10

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

这就是我在使用 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文