1

Why the count of items in my ComboBox is always 0 although the datasource of this combobox has data !!


<div align="right" dir="rtl">
        <asp:Label ID="lbl_contactListName" runat="server" Text="Menu Name :" CssClass="span"></asp:Label>
        <telerik:RadComboBox ID="ddl_contactList" runat="server" AutoPostBack="True" CausesValidation="False"
            CollapseDelay="0" Culture="ar-EG" ExpandDelay="0" Filter="StartsWith" ItemsPerRequest="10"
            MarkFirstMatch="true" Skin="Outlook" EnableAutomaticLoadOnDemand="True" EmptyMessage="-New Menu-"
            ShowMoreResultsBox="True" OnSelectedIndexChanged="ddl_contactList_SelectedIndexChanged"
            EnableVirtualScrolling="True" DataTextField="list_desc" DataValueField="list_code"
            DataSourceID="ObjectDataSource1" EnableViewState="true" Width="300px">
        </telerik:RadComboBox>
    </div>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetContactListsByDep"
            TypeName="SendSMS_EmailModule.ContactList">
            <SelectParameters>
                <asp:SessionParameter Name="year" SessionField="year" Type="Int32" />
                <asp:SessionParameter Name="main_code" SessionField="main_code" Type="Int32" />
            </SelectParameters>
        </asp:ObjectDataSource>
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392

3 Answers3

3

The property value of of Rad combo "EnableAutomaticLoadOnDemand=True" here.This property load all the data on demand. So when you click on your combobox it will load the data in it untill that it is empty. If you do not want to make your combo on demand then Make that property false. By doing that you will get the count directly.

If you want to keep that EnableAutomaticLoadOnDemand property to True. you can use ItemDataBound event of the Rad Combo. By using it you can change the Item's Text and Value properties as well as modify its Attributes collection based on the DataItem

You will find more details at telerik rad combo. Let me know if you want more detail on this.

2

Perhaps you should call DataBind() before you call Count().

ddl_contactList.DataBind();
ddl_contactList.Items.Count();
Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
  • hmmm i use `DataSourceId` so why should i bind again ?? – Anyname Donotcare Nov 20 '12 at 16:36
  • 3
    DataSourceId will come into effect in the `PreRender` event in the page lifecycle, and your `.Count()` call is happening in `Page_Load` which happens before that, so manually calling `.DataBind()` causes it to databind earlier, when you need it. Alternatively you could try to move your `.Count()` to the `PreRender` event – Thymine Nov 20 '12 at 17:00
  • @Henk Mollema: which part ?? – Anyname Donotcare Nov 21 '12 at 08:44
  • I need to try to get the count on a `button` and i write your code in my `page_load()` but always the count is zero . – Anyname Donotcare Nov 21 '12 at 08:45
  • @just_name do you want to call Count() when you click a button or when your page is loaded? – Henk Mollema Nov 21 '12 at 08:47
  • `ddl_contactList.DataBind(); ddl_contactList.Items.Count();` – Anyname Donotcare Nov 21 '12 at 08:55
  • Use this code: `ddl_contactList.DataBind();` `int itemCount = ddl_contactList.Items.Count();` Set a breakpoint to the second line, debug the project and check what value `itemCount` is set to. – Henk Mollema Nov 21 '12 at 09:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19877/discussion-between-henk-mollema-and-just-name) – Henk Mollema Nov 21 '12 at 10:51
2

Are you getting the Count as Zero on Page Load.

If that's the case it is because the page load event hits before the ComboBox is populated. An easier way is to populate the items on Page Load itself.

(This code is untested)

if(!Page.IsPostBack)
{
 using(var context = new Entities())
 {
   foreach(var item in context.Employee)
   {
      RadComboBox1.Items.Add(new RadListBoxItem(item.Name, item.ID.ToString()));
   }
 }
}
//Here you can get the count.
arunlalam
  • 1,838
  • 2
  • 15
  • 23