if (this.cmbTO.CheckedItems.Count != 0) {
//do something
}
this.cmbTO.DataTextField = "UserName";
this.cmbTO.DataValueField = "UserEmail";
this.cmbTO.DataSource = dtTbl;
this.cmbTO.DataBind();
6 Answers, 1 is accepted
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.
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
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
@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.
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