我想添加一个项目到下拉列表的顶部。我正在使用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>
在您的示例中,您正在使用1-item-list覆盖DataSourceObject。
您应该在DataBind
呼叫后添加"手动"项:
myCombo.Items.Insert(0,
new Telerik.Web.UI.RadComboBoxItem { Text = "All Profiles", Value = "1" }
);
myCombo.SelectedIndex = 0;
如果你在绑定模式下工作,所有的数据都应该来自数据源。
我通常做的是在数据源中添加ID为0或int的额外元素。MaxValue,这取决于排序顺序和我想显示它的位置