This is a migrated thread and some comments may be shown as answers.

CheckedItems.Count Always Returns a Value of Zero (0)

6 Answers 878 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 1
Brian asked on 05 Mar 2013, 10:49 PM
I created a .NET page using the demo ComboBox-CheckBoxes (http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/checkboxes/defaultcs.aspx) as a guide. All worked great.
 
Then I added an asp:SqlDataSource to my page with ID="sdsEmailAddresses". I went to the telerik:RadComboBox and set DataSourceID="sdsEmailAddresses" (along with other data source settings). The combobox displayed the desired data and when you selected items in the list and click a button, the click event has the following, which successfully detects the number of items that where selected:

if (this.cmbTO.CheckedItems.Count != 0) {

//do something

}

But I need to use a complex Select Command that needs to be filtered on-the-fly. So instead of using the asp:SqlDataSource, I have a method that sets the radcombobox like the following:
 

this.cmbTO.DataTextField = "UserName";

this.cmbTO.DataValueField = "UserEmail";

this.cmbTO.DataSource = dtTbl;

this.cmbTO.DataBind();

This code properly populates radcombobox, but after making combo selections, and clicking the button, the Count always returns zero.
 
What's up with that?

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 06 Mar 2013, 09:19 AM
Hello Brian,

I am able to replicate your issue by enabling load on demand and hence I believe you are using EnableLoadOnDemand property of the RadComboBox. RadComboBox items are not accessible on the server-side when loading them on demand and therefore always return CheckedItems as well as SelectedItems count as zero and this is a known issue. This is because RadComboBox items loaded on demand using the ItemsRequested event handler or WebService do not exist on the server and cannot be accessed using the server-side FindItemByText / Value methods. SelectedItem and SelectedIndex properties are always Null / Nothing. This is needed for speed (otherwise the combobox will not be that responsive upon each keypress if state information and ViewState were persisted).

Please have a look at the following code without using load on demand which works fine at my end.

ASPX:
<telerik:RadComboBox runat="server" ID="RadComboBox1" CheckBoxes="true">
</telerik:RadComboBox>
<br />
<br />
<telerik:RadButton ID="RadButton1" runat="server" Text="Get Count" OnClick="RadButton1_Click">
</telerik:RadButton>

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        populateRadCombobox("select ContactName from Customers");
    }
}
protected void populateRadCombobox(string query)
{
    String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(query, conn);
 
    DataTable myDataTable = new DataTable();
 
    conn.Open();
    try
    {
        adapter.Fill(myDataTable);
        RadComboBox1.DataTextField = "ContactName";
        RadComboBox1.DataSource = myDataTable;
        RadComboBox1.DataBind();
    }
    finally
    {
        conn.Close();
    }
}
protected void RadButton1_Click(object sender, EventArgs e)
{
    if (RadComboBox1.CheckedItems.Count > 0)
    {
        //business logic goes here
    }
    else
    {
 
    }
}

Thanks,
Princy.
0
Brian
Top achievements
Rank 1
answered on 06 Mar 2013, 01:41 PM

Hi Princy,

 

Thank you for your quick reply.

 

Prior to posting this issue yesterday, I had already read a Telerik document concerning the use of EnableLoadOnDemand property and the ItemsRequested event with regards to RadCombos and server-side access. I had applied the info in that document, but it was not resolving this issue. So I removed all of those changes that I made to my page, including the removal of the EnableLoadOnDemand property. After some frustrations, I then posted this issue.

 

When I reviewed what you had posted, I found that your example was almost exactly like mine, with one difference. You had populateRadCombobox inside of an if (!IsPostBack) statement, and I did not. If you place your call to populateRadCombobox outside of the if (!IsPostBack), you will find that this also causes CheckedItems.Count property to always return a zero.

 

In the past, I’ve used if (!IsPostBack) in the Page_Load event more times than I care to admit; it was an oversight in this case. So even without the use of EnableLoadOnDemand, you can repro this problem if you place populateRadCombobox outside of the if (!IsPostBack).

 

I wonder though; shouldn’t the postback have caused all the items that had a checkmark to become unchecked during the postback? That’s what was driving me crazy; everything I had checked was still checked after I clicked my button, yet the CheckedItems.Count would return a zero.

 

Issue resolved,

Thank you

0
Marie
Top achievements
Rank 1
answered on 10 Sep 2014, 02:10 PM
I'm having the same problem. I'm using EnableLoadOnDemand="True" because I need to have Autocomplete combobox.
I don't want to load everything on load page because I have the long long lists coming from an old server Informix. I need speed and for that I use EnableLoadOnDemand and CheckBoxes="true". for that I use OnItemsRequested
There is another way to do that?
I tried to add to this OnItemChecked but I have another error :

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

If I use SelectedIndexChanged with my autocomplete and loadondemand I have the same problem mentionned here. Only after the second check works. If I check only one doesn't work...

Any suggestion?
Thanks
0
Nencho
Telerik team
answered on 15 Sep 2014, 10:16 AM
Hello,

@Brian, indeed the cause of the experienced issue is the fact, that each time the Page_Load event is hit, you are repopulating the control with data. Thus, the selection, along with the client-state of the RadComboBox is lost and the Checked Items count is zero. I would suggest you to encapsulate the population with data, within the If(!IsPostBack) statement, in order to persist the correct behavior.

@Marie, as previously mentioned by Princy, the LoadOnDemand mechanism is not supported along with the CheckBoxes usage. This is described in our help article bellow:

http://www.telerik.com/help/aspnet-ajax/combobox-usability-checkboxes.html

However, another possible implementation might be helpful for your scenario. You could still use LoadOnDemand mechanism of the RadComboBox and place a checkbox in the ItemTemplate of the control. Please refer to the following forum thread, where this subject is discussed in details and a sample project is provided:

http://www.telerik.com/forums/radcombo-multi-select-and-load-on-demand

Regards,
Nencho
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Dan
Top achievements
Rank 1
answered on 23 Jun 2016, 08:24 PM
Hi I removed the LoadOnDemand and it still returns 0 on count
0
Nencho
Telerik team
answered on 28 Jun 2016, 12:42 PM
Hello Dan,

Since I am not able to locally replicate the issue in question in this thread, when the LoadOnDemand feature is disabled, and as I can see you have already submitted a support ticket on the matter - I would suggest you to continue the correspondence in the support ticket.

Regards,
Nencho
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
ComboBox
Asked by
Brian
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Brian
Top achievements
Rank 1
Marie
Top achievements
Rank 1
Nencho
Telerik team
Dan
Top achievements
Rank 1
Share this question
or