Search code examples
asp.netteleriktelerik-ajax

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


I have Use telerik:radcombobox with mutiple select value.I have bind data LoadOndemand.All Work Fine but when i click on submit button then CheckedItems.Count=0.

Thank you,

Dhiren Patel


Solution

  • 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.

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

    Code Behind:

    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
    {
    
    }
    

    Reference: http://www.telerik.com/forums/checkeditems-count-always-returns-a-value-of-zero-0

    http://www.telerik.com/forums/radcombobox-losing-client-selections-on-postback