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

7 posts, 0 answers
  1. Brian
    Brian avatar
    68 posts
    Member since:
    Jun 2011

    Posted 05 Mar 2013 Link to this post

    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?
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 06 Mar 2013 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Brian
    Brian avatar
    68 posts
    Member since:
    Jun 2011

    Posted 06 Mar 2013 Link to this post

    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

  5. Marie
    Marie avatar
    9 posts
    Member since:
    May 2014

    Posted 10 Sep 2014 Link to this post

    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
  6. Nencho
    Admin
    Nencho avatar
    1458 posts

    Posted 15 Sep 2014 Link to this post

    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.

     
  7. Dan
    Dan avatar
    2 posts
    Member since:
    Jun 2016

    Posted 23 Jun in reply to Princy Link to this post

    Hi I removed the LoadOnDemand and it still returns 0 on count
  8. Nencho
    Admin
    Nencho avatar
    1458 posts

    Posted 28 Jun Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017