2011-07-08 122 views
4

我正在嘗試將項目添加到下拉菜單的頂部。我使用的ItemTemplate,所以我做了數據綁定,並嘗試添加一個在讀取RadComboBox將項目添加到頂部

[ ] 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> 

回答

8

在你的例子中,你用你的1項目列表覆蓋你的DataSourceObject。

您應該將DataBind調用後添加「手動」的項目:

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

我試過,但它告訴我下面的內容:「Telerik.Web.UI.RadComboBoxItemCollection.Insert的最佳重載的方法匹配( int,Telerik.Web.UI.RadComboBoxItem)有一些無效參數。 – user710502

+0

因此,只需嘗試:'new Telerik.Web.UI.RadComboBoxItem {'...改爲使用RadComboBoxItem的正確屬性名稱 – splattne

+0

好吧,我試過了,它確實顯示了複選框,點擊時字符串顯示「All Profiles 「但問題是,在組合框中它顯示覆選框,標籤是空的,這個標籤是在一個ItemTemplate中..我怎麼能通過文本的項目templace,以便它顯示覆選框,並在它旁邊的標籤? - 對不起,我是一種新手:) 張貼代碼上面 – user710502

0

如果在綁定模式下工作,所有的數據應該來自數據源。

我通常會做的是在ID爲0或int.MaxValue的數據源中添加一個額外元素,具體取決於排序順序和我想要顯示的位置。

相關問題