【问题标题】:RadComboBox adding an item to the topRadComboBox 将项目添加到顶部
【发布时间】:2011-07-08 14:10:20
【问题描述】:

我正在尝试将一个项目添加到下拉列表的顶部。我正在使用 ItemTemplates 所以我正在做数据绑定并尝试在顶部添加一个读取

[ ] All Profiles

我能够添加它,但它覆盖了实际数据的绑定,所以当我现在添加它时,只有[ ] All profiles 存在,但没有真正的绑定数据。我究竟做错了什么?

顺便说一句,我是 c# 的新手 :)

谢谢

  public void BindData()
{
    myCombo.DataSource = myDbConnection.GetValues();
    myCombo.DataTextField = "Name";
    myCombo.DataValueField = "ID";
    myCombo.DataBind();
    var tempProfiles = new[] { new { Name = "All Profiles", ID = "1" } };
    myCombo.DataSource = tempProfiles;
    myCombo.DataBind();

}

<telerik:RadComboBox ID="myCombo" EmptyMessage="All Types" runat="server" Width="200px">
     <ItemTemplate>
       <div onclick="StopPropagation(event)">
         <asp:CheckBox runat="server" ID="chk1" onclick="onCheckBoxClick(this)"/>
              <asp:Label runat="server" ID="lblProfile" AssociatedControlID="chk1">
                  <%# Eval("Name") %>
               </asp:Label>
             </div>
            </ItemTemplate>
            </telerik:RadComboBox> 

【问题讨论】:

    标签: c# asp.net telerik


    【解决方案1】:

    在您的示例中,您将使用 1-item-list 覆盖 DataSourceObject。

    您应该在DataBind 调用之后“手动”添加项目:

     myCombo.Items.Insert(0, 
           new Telerik.Web.UI.RadComboBoxItem { Text = "All Profiles", Value = "1" }
        );
     myCombo.SelectedIndex = 0;
    

    【讨论】:

    • 我试过这个,但它告诉我以下内容:'Telerik.Web.UI.RadComboBoxItemCollection.Insert(int, Telerik.Web.UI.RadComboBoxItem) 的最佳重载方法匹配有一些无效参数。
    • 所以,只需尝试:new Telerik.Web.UI.RadComboBoxItem { ... 并使用 RadComboBoxItem 的正确属性名称
    • 好的,我试过了,它确实显示了复选框,当点击字符串显示“所有配置文件”时,问题是在组合框中它显示了复选框并且标签为空,这个标签在一个 ItemTemplate .. 如何传递项目模板的文本,以便它显示复选框并在其旁边显示标签? - 对不起,我是一个新手 :) 上面发布的代码
    • 你必须设置正确的属性,我猜它们是TextValue。我会研究它并稍后编辑我的答案。
    【解决方案2】:

    如果您在 Bound 模式下工作,所有数据都应该来自 DataSource。

    我通常做的是在数据源中添加一个 ID 为 0 或 int.MaxValue 的额外元素,具体取决于排序顺序和我想要显示的位置。

    【讨论】:

      猜你喜欢
      • 2013-05-06
      • 1970-01-01
      • 2018-02-05
      • 2016-12-15
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      相关资源
      最近更新 更多