Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# RadComboBox将项目添加到顶部_C#_Asp.net_Telerik - Fatal编程技术网

C# RadComboBox将项目添加到顶部

C# RadComboBox将项目添加到顶部,c#,asp.net,telerik,C#,Asp.net,Telerik,我正在尝试将项目添加到下拉列表的顶部。我正在使用ItemTemplates,所以我正在进行数据绑定,并试图在顶部添加一个 [ ] All Profiles 我可以添加它,但它覆盖了实际数据的绑定,因此,当我现在添加它时,只有[]所有配置文件在那里,而不是真正的绑定数据。我做错了什么 顺便说一句,我是c的新手 多谢各位 public void BindData() { myCombo.DataSource = myDbConnection.GetValues(); myCom

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

[ ] 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> 
public void BindData()
{
myCombo.DataSource=myDbConnection.GetValues();
myCombo.DataTextField=“Name”;
myCombo.DataValueField=“ID”;
myCombo.DataBind();
var temprofiles=new[]{new{Name=“All Profiles”,ID=“1”};
myCombo.DataSource=tempProfiles;
myCombo.DataBind();
}

如果在绑定模式下工作,所有数据都应该来自数据源


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

在您的示例中,您正在使用1项列表覆盖DataSourceObject

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

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

我试过了,但它告诉我:与'Telerik.Web.UI.RadComboxItemCollection.Insert(int,Telerik.Web.UI.RadComboxItem)匹配的最佳重载方法(int,Telerik.Web.UI.RadComboxItem)有一些无效参数。因此,请尝试:
new Telerik.Web.UI.RadComboxItem{
…取而代之,使用RadComboxItemOK的正确属性名。我尝试了这个方法,它确实显示了复选框,单击时字符串显示“所有配置文件”但问题是,在组合框中,它显示复选框,标签为空,此标签位于ItemTemplate内..如何传递ItemTemplate的文本,使其显示复选框并在其旁边显示标签?-抱歉,我是一个新手:)上面发布的代码您必须设置正确的属性,我猜它们是
Text
Value
。我将研究它,稍后编辑我的答案。